Skip to content

Commit c961b90

Browse files
committed
Docs: Updated versions and added case class quick tour example
1 parent d1b1975 commit c961b90

File tree

12 files changed

+322
-14
lines changed

12 files changed

+322
-14
lines changed

docs/landing/data/releases.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
driverName = "mongo-scala-driver"
22
githubRepo = "mongo-scala-driver"
3-
current = "1.2.1"
3+
current = "2.0.0"
44

55
[[versions]]
6-
version = "1.2.1"
6+
version = "2.0.0"
77
status = "current"
8+
docs = "./2.0"
9+
api = "./2.0/scaladoc"
10+
11+
12+
[[versions]]
13+
version = "1.2.1"
814
docs = "./1.2"
915
api = "./1.2/scaladoc"
1016

docs/landing/static/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"version": "1.2"}, {"version": "1.1"}, {"version": "1.0"}]
1+
[{"version": "2.0"}, {"version": "1.2"}, {"version": "1.1"}, {"version": "1.0"}]

docs/reference/config.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
baseurl = "/mongo-scala-driver/1.2"
1+
baseurl = "/mongo-scala-driver/2.0"
22
languageCode = "en-us"
33
title = "MongoDB Scala Driver"
44
theme = "mongodb"
@@ -11,13 +11,13 @@ canonifyurls = false
1111
[[menu.main]]
1212
name = "API Documentation"
1313
pre = "<i class='fa fa-file-text-o'></i>"
14-
weight = 90
14+
weight = 100
1515
identifier = "apiDocs"
1616
url = "/scaladoc"
1717

1818
[[menu.main]]
1919
name = "Source Code"
2020
pre = "<i class='fa fa-github'></i>"
21-
weight = 90
21+
weight = 100
2222
identifier = "githubLink"
2323
url = "https://github.com/mongodb/mongo-scala-driver/tree/master"

docs/reference/content/getting-started/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ To help you get started quickly on the new driver, follow:
1414
* [Installation]({{< ref "getting-started/installation-guide.md" >}}), how to install the Scala driver.
1515
* [Quick Tour Primer]({{< ref "getting-started/quick-tour-primer.md" >}}), background information on the driver and its asynchronous API.
1616
* [Quick Tour]({{< ref "getting-started/quick-tour.md" >}}), an introduction to using the driver for CRUD operations.
17+
* [Quick Tour - case classes]({{< ref "getting-started/quick-tour-case-classes.md" >}}), an introduction to using the driver with case classes.
1718
* [Admin Quick Tour]({{< ref "getting-started/quick-tour-admin.md" >}}), an introduction to using the driver for administrative operations.

docs/reference/content/getting-started/installation-guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ The MongoDB Scala Driver
1919

2020
### Scala 2.12
2121

22-
{{< install artifactId="mongo-scala-driver" version="1.2.1" mongoDriverVersion="3.4.0" scalaVersion="2.12">}}
22+
{{< install artifactId="mongo-scala-driver" version="2.0.0" mongoDriverVersion="3.4.2" scalaVersion="2.12">}}
2323

2424
### Scala 2.11
2525

26-
{{< install artifactId="mongo-scala-driver" version="1.2.1" mongoDriverVersion="3.4.0" scalaVersion="2.11">}}
26+
{{< install artifactId="mongo-scala-driver" version="2.0.0" mongoDriverVersion="3.4.2" scalaVersion="2.11">}}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
+++
2+
date = "2015-03-17T15:36:56Z"
3+
title = "Quick Tour - case classes"
4+
[menu.main]
5+
parent = "Getting Started"
6+
identifier = "Quick Tour - case classes"
7+
weight = 35
8+
pre = "<i class='fa'></i>"
9+
+++
10+
11+
# Quick Tour with case classes
12+
13+
The following code snippets come from the `QuickTourCaseClass.scala` example code that can be found with the
14+
[driver source]({{< srcref "examples/src/test/scala/tour/QuickTourCaseClass.scala">}}).
15+
16+
{{% note class="important" %}}
17+
This follows on from the [quick tour]({{< relref "getting-started/quick-tour.md" >}}).
18+
19+
See the [Bson macros]({{< relref "bson/macros.md" >}}) documentation for in-depth information about using macros for configuring case class
20+
support with your `MongoCollection`.
21+
{{% /note %}}
22+
23+
24+
First we'll create the case class we want to use to represent the documents in the collection. In the following we create a `Person` case
25+
class and companion object:
26+
27+
```scala
28+
import org.mongodb.scala.bson.ObjectId
29+
object Person {
30+
def apply(firstName: String, lastName: String): Person =
31+
Person(new ObjectId(), firstName, lastName)
32+
}
33+
case class Person(_id: ObjectId, firstName: String, lastName: String)
34+
```
35+
36+
{{% note %}}
37+
You'll notice in the companion object we automatically assign a `_id` when creating new instances that don't include it. The `_id` is the
38+
primary key for a document, so by having a `_id` field in the case class it allows access to the primary key.
39+
{{% /note %}}
40+
41+
## Configuring case classes
42+
43+
Then when using `Person` with our collection we must have a `Codec` that can convert it to and from `BSON`. The `bson` package provides
44+
macros that automatically generate a codec at compile time. In the following example we create a new `CodecRegistry` that includes a
45+
codec for the `Person` case class:
46+
47+
48+
```scala
49+
import org.mongodb.scala.bson.codecs.Macros._
50+
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
51+
import org.bson.codecs.configuration.CodecRegistries.{fromRegistries, fromProviders}
52+
53+
val codecRegistry = fromRegistries(fromProviders(classOf[Person]), DEFAULT_CODEC_REGISTRY )
54+
```
55+
56+
Once we have the `codecRegistry` configured then we can use that to create a `MongoCollection[Person]`. In the following we get the `test` collection on the `mydb` database. Notice the `codecRegistry` is set at the database level, it could have been set when creating the `MongoClient` or even via the `MongoCollection.withCodecRegistry` method.
57+
58+
```scala
59+
// To directly connect to the default server localhost on port 27017
60+
val mongoClient: MongoClient = MongoClient()
61+
val database: MongoDatabase = mongoClient.getDatabase("mydb").withCodecRegistry(codecRegistry)
62+
val collection: MongoCollection[Person] = database.getCollection("test")
63+
```
64+
65+
## Insert a person
66+
67+
Once you have the collection object, you can insert `Person` instances into the collection:
68+
69+
```scala
70+
val person: Person = Person("Ada", "Lovelace")
71+
collection.insertOne(person).results()
72+
```
73+
74+
## Add multiple instances
75+
76+
To add multiple `Person` instances, you can use the `insertMany()` method and print the results of the operation:
77+
78+
```scala
79+
val people: Seq[Person] = Seq(
80+
Person("Charles", "Babbage"),
81+
Person("George", "Boole"),
82+
Person("Gertrude", "Blanch"),
83+
Person("Grace", "Hopper"),
84+
Person("Ida", "Rhodes"),
85+
Person("Jean", "Bartik"),
86+
Person("John", "Backus"),
87+
Person("Lucy", "Sanders"),
88+
Person("Tim", "Berners Lee"),
89+
Person("Zaphod", "Beeblebrox")
90+
)
91+
collection.insertMany(people).printResults()
92+
```
93+
And we should the following output:
94+
95+
```
96+
The operation completed successfully
97+
```
98+
99+
## Querying the collection
100+
101+
Use the [find()]({{< apiref "org.mongodb.scala.MongoCollection@find[C](filter:org.bson.conversions.Bson)(implicite:org.mongodb.scala.Helpers.DefaultsTo[C,org.mongodb.scala.collection.immutable.Document],implicitct:scala.reflect.ClassTag[C]):org.mongodb.scala.FindObservable[C]">}})
102+
method to query the collection.
103+
104+
### Find the First Person in a Collection
105+
106+
You can query the collection in the same as shown in the [quick tour]({{< relref "getting-started/quick-tour.md" >}}).
107+
108+
```scala
109+
collection.find().first().printHeadResult()
110+
```
111+
112+
The example will print the first `Person` in the database:
113+
114+
```
115+
Person(58dd0a68218de22333435fa4, Ada, Lovelace)
116+
```
117+
118+
### Find all people in the collection
119+
120+
To retrieve all the people in the collection, we will use the
121+
`find()` method. The `find()` method returns a `FindObservable` instance that
122+
provides a fluent interface for chaining or controlling find operations.
123+
The following code retrieves all documents in the collection and prints them out
124+
(101 documents). Using the `printResults()` implicit we block until the observer is completed and then print each result:
125+
```scala
126+
collection.find().printResults()
127+
```
128+
129+
130+
## Get a single person with a query filter
131+
132+
We can create a filter to pass to the find() method to get a subset of
133+
the documents in our collection. For example, if we wanted to find the
134+
document for which the value of the "firstName" is "Ida", we would do the
135+
following:
136+
137+
```scala
138+
import org.mongodb.scala.model.Filters._
139+
140+
collection.find(equal("firstName", "Ida")).first().printHeadResult()
141+
```
142+
143+
will eventually print just one Person:
144+
145+
```
146+
Person(58dd0a68218de22333435fa4, Ida, Rhodes)
147+
```
148+
149+
{{% note %}}
150+
Use the [`Filters`]({{< relref "builders/filters.md">}}), [`Sorts`]({{< relref "builders/sorts.md">}}),
151+
[`Projections`]({{< relref "builders/projections.md">}}) and [`Updates`]({{< relref "builders/updates.md">}})
152+
helpers for simple and concise ways of building up queries.
153+
{{% /note %}}
154+
155+
## Get a set of people with a query
156+
157+
We can use the query to get a set of people from our collection. For
158+
example, if we wanted to get all documents where the `firstName` starts with `G` and sort by `lastName` we could
159+
write:
160+
161+
```scala
162+
collection.find(regex("firstName", "^G")).sort(ascending("lastName")).printResults()
163+
```
164+
165+
Which will print out the Person instances for Gertrude, George and Grace.
166+
167+
## Updating documents
168+
169+
There are numerous [update operators](http://docs.mongodb.org/manual/reference/operator/update-field/)
170+
supported by MongoDB. We can use the [Updates]({{< apiref "org.mongodb.scala.model.Updates$">}}) helpers to help update documents in the database.
171+
172+
In the following we update and correct the hyphenation of Tim Berners-Lee:
173+
174+
```scala
175+
collection.updateOne(equal("lastName", "Berners Lee"), set("lastName", "Berners-Lee")).printHeadResult("Update Result: ")
176+
```
177+
178+
The update methods return an [`UpdateResult`]({{< coreapiref "com/mongodb/client/result/UpdateResult.html">}}),
179+
which provides information about the operation including the number of documents modified by the update.
180+
181+
## Deleting documents
182+
183+
To delete at most a single document (may be 0 if none match the filter) use the [`deleteOne`]({{< apiref "org.mongodb.scala.MongoCollection@deleteOne(filter:org.bson.conversions.Bson):org.mongodb.scala.Observable[org.mongodb.scala.result.DeleteResult]">}})
184+
method:
185+
186+
```scala
187+
collection.deleteOne(equal("firstName", "Zaphod")).printHeadResult("Delete Result: ")
188+
```
189+
190+
As you can see the API allows for easy use of CRUD operations with case classes. See the [Bson macros]({{< relref "bson/macros.md" >}})
191+
documentation for further information about the macros.

docs/reference/content/upgrade.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
22
date = "2015-11-18T09:56:14Z"
3-
title = "Changelog"
3+
title = "Upgrading"
44
[menu.main]
55
weight = 95
66
pre = "<i class='fa fa-wrench'></i>"

docs/reference/data/mongodb.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
githubUser = "mongodb"
22
githubRepo = "mongo-scala-driver"
33
githubBranch = "master"
4-
currentVersion = "1.2"
4+
currentVersion = "2.0"
55
highlightTheme = "idea.css"
66
apiUrl = "/scaladoc/index.html"
77
coreApiUrl = "http://api.mongodb.org/java/3.4/"
88
coreDocsUrl = "http://mongodb.github.io/mongo-java-driver/3.4/"
9-
mongoDriverVersion = "3.4.0" # Update in installation-guide.md
9+
mongoDriverVersion = "3.4.2" # Update in installation-guide.md

examples/src/test/scala/tour/GridFSTour.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import org.mongodb.scala._
3333
import tour.Helpers._
3434

3535
/**
36-
* The GridFSTour code example see: https://mongodb.github.io/mongo-scala-driver/1.2/getting-started
36+
* The GridFSTour code example see: https://mongodb.github.io/mongo-scala-driver/2.0/getting-started
3737
*/
3838
object GridFSTour {
3939

examples/src/test/scala/tour/QuickTour.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import org.mongodb.scala.model._
2929
import tour.Helpers._
3030

3131
/**
32-
* The QuickTour code example see: https://mongodb.github.io/mongo-scala-driver/1.0/getting-started
32+
* The QuickTour code example see: https://mongodb.github.io/mongo-scala-driver/2.0/getting-started
3333
*/
3434
object QuickTour {
35+
//scalastyle:off method.length
3536

3637
/**
3738
* Run this main method to see the output of this quick example.

0 commit comments

Comments
 (0)