@@ -40,14 +40,14 @@ If you are using `Gradle <https://gradle.org/>`__, add the following to your
40
40
41
41
.. include:: /includes/quick-start/gradle-versioned.rst
42
42
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
45
45
the project in your IDE.
46
46
47
47
.. 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
49
49
file. Refer to the documentation provided by maintainers of your IDE
50
- and/or dependency management system.
50
+ and dependency management system.
51
51
52
52
For example, if you build with Maven in the IntelliJ IDE, you need to
53
53
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``
75
75
in the base package directory of your project. Use the following sample
76
76
code to run a query on your sample dataset in MongoDB Atlas, replacing the
77
77
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
79
79
the password you created for your user that has **atlasAdmin** permissions:
80
80
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:
82
86
83
- import static com.mongodb.client.model.Filters.eq;
87
+ .. include:: /includes/quick-start/query-output.rst
84
88
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.
86
92
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
95
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.
93
100
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:
96
104
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:
99
110
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:
102
117
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:
105
123
106
- .. include:: /includes/quick-start/query-output.rst
124
+ .. include:: /includes/quick-start/pojo- query-output.rst
107
125
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:
111
128
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>`
112
131
113
132
Next steps
114
133
----------
0 commit comments