Skip to content

Commit 3bfaefa

Browse files
author
Chris Cho
authored
DOCSP-12160: quickstart pojos (#94)
* DOCSP-12160: quickstart pojos
1 parent c7487d0 commit 3bfaefa

File tree

8 files changed

+159
-29
lines changed

8 files changed

+159
-29
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fundamentals;
2+
3+
import java.util.List;
4+
5+
6+
// begin moviePojo
7+
public class Movie {
8+
String plot;
9+
List<String> genres;
10+
String title;
11+
12+
public String getPlot() {
13+
return plot;
14+
}
15+
public void setPlot(String plot) {
16+
this.plot = plot;
17+
}
18+
public List<String> getGenres() {
19+
return genres;
20+
}
21+
public void setGenres(List<String> genres) {
22+
this.genres = genres;
23+
}
24+
public String getTitle() {
25+
return title;
26+
}
27+
public void setTitle(String title) {
28+
this.title = title;
29+
}
30+
@Override
31+
public String toString() {
32+
return "Movie [\n plot=" + plot + ",\n genres=" + genres + ",\n title=" + title + "\n]";
33+
}
34+
}
35+
// end moviePojo
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fundamentals;
2+
3+
// begin QuickStart
4+
import static com.mongodb.client.model.Filters.eq;
5+
6+
import org.bson.Document;
7+
8+
import com.mongodb.client.MongoClient;
9+
import com.mongodb.client.MongoClients;
10+
import com.mongodb.client.MongoCollection;
11+
import com.mongodb.client.MongoDatabase;
12+
13+
public static void main( String[] args ) {
14+
15+
// Replace the uri string with your MongoDB deployment's connection string
16+
String uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
17+
18+
try (MongoClient mongoClient = MongoClients.create(uri)) {
19+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
20+
MongoCollection<Document> collection = database.getCollection("movies");
21+
22+
Document doc = collection.find(eq("title", "Back to the Future")).first();
23+
System.out.println(doc.toJson());
24+
}
25+
}
26+
// end QuickStart
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fundamentals;
2+
3+
// begin PojoQuickstart
4+
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
5+
import static com.mongodb.client.model.Filters.eq;
6+
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
7+
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
8+
9+
import org.bson.codecs.configuration.CodecProvider;
10+
import org.bson.codecs.configuration.CodecRegistry;
11+
import org.bson.codecs.pojo.PojoCodecProvider;
12+
13+
import com.mongodb.client.MongoClient;
14+
import com.mongodb.client.MongoClients;
15+
import com.mongodb.client.MongoCollection;
16+
import com.mongodb.client.MongoDatabase;
17+
18+
public class QuickStartPojoExample {
19+
20+
public static void main(String[] args) {
21+
CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
22+
CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
23+
24+
// Replace the uri string with your MongoDB deployment's connection string
25+
String uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
26+
27+
try (MongoClient mongoClient = MongoClients.create(uri)) {
28+
MongoDatabase database = mongoClient.getDatabase("sample_mflix").withCodecRegistry(pojoCodecRegistry);
29+
MongoCollection<Movie> collection = database.getCollection("movies", Movie.class);
30+
31+
Movie movie = collection.find(eq("title", "Back to the Future")).first();
32+
System.out.println(movie);
33+
}
34+
}
35+
}
36+
// end PojoQuickstart
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. code-block:: groovy
22
33
dependencies {
4-
compile 'org.mongodb:mongodb-driver-sync:4.1.0'
4+
compile 'org.mongodb:mongodb-driver-sync:4.3.0'
55
}
66

source/includes/quick-start/maven-versioned.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<dependency>
55
<groupId>org.mongodb</groupId>
66
<artifactId>mongodb-driver-sync</artifactId>
7-
<version>4.1.0</version>
7+
<version>4.3.0</version>
88
</dependency>
99
</dependencies>
1010
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
When you run the ``QuickStartPojoExample`` class, it should output the details of the
2+
movie from the sample dataset which will look something like this:
3+
4+
.. code-block:: none
5+
6+
Movie [
7+
plot=A young man is accidentally sent 30 years into the past...,
8+
genres=[Adventure, Comedy, Sci-Fi],
9+
title=Back to the Future
10+
]
11+
12+
If you receive no output or an error, check whether you included the proper
13+
connection string in your Java class, and whether you loaded the sample dataset
14+
into your MongoDB Atlas cluster.

source/includes/quick-start/query-output.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
When you run the ``QuickStart`` class, it should output the details of the
1+
When you run the ``QuickStart`` class, it should output the details of the
22
movie from the sample dataset which will look something like this:
33

44
.. code-block:: json
@@ -14,4 +14,4 @@ movie from the sample dataset which will look something like this:
1414
1515
If you receive no output or an error, check whether you included the proper
1616
connection string in your Java class, and whether you loaded the sample dataset
17-
in your MongoDB Atlas cluster
17+
into your MongoDB Atlas cluster.

source/quick-start.txt

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ If you are using `Gradle <https://gradle.org/>`__, add the following to your
4040

4141
.. include:: /includes/quick-start/gradle-versioned.rst
4242

43-
Once you configure your dependencies, make sure they are available to your
44-
project which involves running your dependency manager and/or refreshing
43+
Once you configure your dependencies, ensure they are available to your
44+
project which may require running your dependency manager and refreshing
4545
the project in your IDE.
4646

4747
.. note::
48-
You may need to configure the JDK version in your IDE and/or dependency
48+
You may need to configure the JDK version in your IDE and dependency
4949
file. Refer to the documentation provided by maintainers of your IDE
50-
and/or dependency management system.
50+
and dependency management system.
5151

5252
For example, if you build with Maven in the IntelliJ IDE, you need to
5353
specify your JDK version explicitly in the ``properties`` section in your
@@ -75,40 +75,59 @@ Next, create a file to contain your application called ``QuickStart.java``
7575
in the base package directory of your project. Use the following sample
7676
code to run a query on your sample dataset in MongoDB Atlas, replacing the
7777
value of the ``uri`` variable with your MongoDB Atlas connection string.
78-
Make sure to replace the "<password>" section of the connection string with
78+
Ensure you replace the "<password>" section of the connection string with
7979
the password you created for your user that has **atlasAdmin** permissions:
8080

81-
.. code-block:: java
81+
.. literalinclude:: /includes/quick-start/code-snippets/QuickStart.java
82+
:start-after: begin QuickStart
83+
:end-before: end QuickStart
84+
:language: java
85+
:dedent:
8286

83-
import static com.mongodb.client.model.Filters.eq;
87+
.. include:: /includes/quick-start/query-output.rst
8488

85-
import org.bson.Document;
89+
After completing this step, you should have a working application that uses
90+
the Java driver to connect to your MongoDB cluster, run a query on the
91+
sample data, and print out the result.
8692

87-
import com.mongodb.client.MongoClient;
88-
import com.mongodb.client.MongoClients;
89-
import com.mongodb.client.MongoCollection;
90-
import com.mongodb.client.MongoDatabase;
93+
Working with POJOs (Optional)
94+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9195

92-
public static void main( String[] args ) {
96+
In the previous section, you ran a query on a sample collection to retrieve
97+
data in the map-like class ``Document``. In this section, you can learn to
98+
use your own Plain Old Java Object (POJO) to store and retrieve data from
99+
MongoDB.
93100

94-
String uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
95-
MongoClient mongoClient = MongoClients.create(uri);
101+
Create a file called ``Movie.java`` in the base package directory of your
102+
project and add the following code for a class that includes the following
103+
fields, setters, and getters:
96104

97-
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
98-
MongoCollection<Document> collection = database.getCollection("movies");
105+
.. literalinclude:: /includes/quick-start/code-snippets/Movie.java
106+
:start-after: begin moviePojo
107+
:end-before: end moviePojo
108+
:language: java
109+
:dedent:
99110

100-
Document doc = collection.find(eq("title", "Back to the Future")).first();
101-
System.out.println(doc.toJson());
111+
Create a new file ``QuickStartPojoExample.java`` in the same package
112+
directory as your ``Movie`` file in your project. Use the following sample
113+
code to run a query on your sample dataset in MongoDB Atlas, replacing the
114+
value of the ``uri`` variable with your MongoDB Atlas connection string.
115+
Ensure you replace the "<password>" section of the connection string with
116+
the password you created for your user that has **atlasAdmin** permissions:
102117

103-
mongoClient.close();
104-
}
118+
.. literalinclude:: /includes/quick-start/code-snippets/QuickStartPojoExample.java
119+
:start-after: begin PojoQuickstart
120+
:end-before: end PojoQuickstart
121+
:language: java
122+
:dedent:
105123

106-
.. include:: /includes/quick-start/query-output.rst
124+
.. include:: /includes/quick-start/pojo-query-output.rst
107125

108-
After completing this step, you should have a working application that uses
109-
the Java driver to connect to your MongoDB cluster, run a query on the
110-
sample data, and print out the result.
126+
See the following links for more information on using POJOs to store and
127+
retrieve data:
111128

129+
- :doc:`Guide on using POJOs to store and retrieve data</fundamentals/data-formats/document-data-format-pojo>`
130+
- :doc:`Guide on custom serialization of POJOs </fundamentals/data-formats/pojo-customization>`
112131

113132
Next steps
114133
----------

0 commit comments

Comments
 (0)