Skip to content

Commit f0bf0ec

Browse files
Docsp 9930 legacy api (#91)
1 parent 1561edd commit f0bf0ec

18 files changed

+216
-44
lines changed

source/faq.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,97 @@ POJO class:
149149
If you are unable to find the answer to your question here, try our forums and
150150
support channels listed in the :doc:`Issues and Help <issues-and-help>`
151151
section.
152+
153+
154+
Legacy API
155+
----------
156+
157+
.. _faq-legacy-connection:
158+
159+
How do I connect to my MongoDB instance with the legacy API?
160+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161+
162+
The following example shows how to connect to a MongoDB instance with the
163+
legacy API and the current API.
164+
165+
Imagine we are connecting to a collection that contains only this document:
166+
167+
.. code-block:: json
168+
:copyable: false
169+
170+
{"_id": 1, "val": 1}
171+
172+
.. tabs::
173+
174+
.. tab:: Legacy API
175+
:tabid: legacy
176+
177+
.. literalinclude:: /includes/fundamentals/code-snippets/LegacyAPI.java
178+
:language: java
179+
:dedent:
180+
:start-after: start legacy-api-example
181+
:end-before: end legacy-api-example
182+
183+
.. tab:: Current API
184+
:tabid: current
185+
186+
.. literalinclude:: /includes/fundamentals/code-snippets/CurrentAPI.java
187+
:language: java
188+
:dedent:
189+
:start-after: start current-api-example
190+
:end-before: end current-api-example
191+
192+
The output of the above code snippet should look like this:
193+
194+
.. code-block:: json
195+
:copyable: false
196+
197+
{"_id": 1, "val": 1}
198+
199+
For more information on the legacy classes and methods used in the above example,
200+
see the following API documentation pages:
201+
202+
- :java-docs:`Legacy API Javadoc Site <apidocs/mongodb-driver-legacy/index.html>`
203+
- :java-docs:`MongoClient <apidocs/mongodb-driver-legacy/com/mongodb/MongoClient.html>`
204+
- :java-docs:`DB <apidocs/mongodb-driver-legacy/com/mongodb/DB.html>`
205+
- :java-docs:`DBCollection <apidocs/mongodb-driver-legacy/com/mongodb/DBCollection.html>`
206+
- :java-docs:`DBObject <apidocs/mongodb-driver-core/com/mongodb/DBObject.html>`
207+
- :java-docs:`getDB() <apidocs/mongodb-driver-legacy/com/mongodb/MongoClient.html#getDB(java.lang.String)>`
208+
- :java-docs:`getCollection() <apidocs/mongodb-driver-legacy/com/mongodb/DB.html#getCollection(java.lang.String)>`
209+
- :java-docs:`find() <apidocs/mongodb-driver-legacy/com/mongodb/DBCollection.html#find()>`
210+
- :java-docs:`one() <apidocs/mongodb-driver-legacy/com/mongodb/DBCursor.html#one()>`
211+
212+
213+
How do I use the legacy ``MongoClientOptions`` and ``MongoClientURI`` Classes?
214+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215+
216+
Here is an example showing how to use the legacy ``MongoClientOptions`` and
217+
``MongoClientURI`` classes to set your write concern:
218+
219+
.. tabs::
220+
221+
.. tab:: Legacy API
222+
:tabid: legacy
223+
224+
.. literalinclude:: /includes/fundamentals/code-snippets/LegacyAPI.java
225+
:language: java
226+
:dedent:
227+
:start-after: start legacy-api-mongoclientoptions-example
228+
:end-before: end legacy-api-mongoclientoptions-example
229+
230+
.. tab:: Current API
231+
:tabid: current
232+
233+
.. literalinclude:: /includes/fundamentals/code-snippets/CurrentAPI.java
234+
:language: java
235+
:dedent:
236+
:start-after: start current-api-mongoclientsettings-example
237+
:end-before: end current-api-mongoclientsettings-example
238+
239+
For more information on the legacy classes and methods used in the above example,
240+
see the following API documentation pages:
241+
242+
- :java-docs:`Legacy API Javadoc Site <apidocs/mongodb-driver-legacy/index.html>`
243+
- :java-docs:`MongoClient <apidocs/mongodb-driver-legacy/com/mongodb/MongoClient.html>`
244+
- :java-docs:`MongoClientOptions <apidocs/mongodb-driver-legacy/com/mongodb/MongoClientOptions.html>`
245+
- :java-docs:`MongoClientURI <apidocs/mongodb-driver-legacy/com/mongodb/MongoClientURI.html>`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.mycompany.app;
2+
3+
4+
import com.mongodb.*;
5+
import com.mongodb.client.MongoClient;
6+
import com.mongodb.client.MongoClients;
7+
import com.mongodb.client.MongoCollection;
8+
import com.mongodb.client.MongoDatabase;
9+
import org.bson.Document;
10+
import org.bson.conversions.Bson;
11+
12+
import java.net.URL;
13+
14+
public class CurrentAPI {
15+
16+
private static final String COLLECTION = "test";
17+
private static final String DATABASE = "test";
18+
private static String URI = System.getenv("DRIVER_URL");
19+
20+
public static void main(String[] args) throws InterruptedException {
21+
CurrentAPI c = new CurrentAPI();
22+
c.example1();
23+
c.example2();
24+
25+
}
26+
27+
private void example1() {
28+
// start current-api-example
29+
MongoClient client = MongoClients.create(URI);
30+
MongoDatabase db = client.getDatabase(DATABASE);
31+
MongoCollection<Document> col = db.getCollection(COLLECTION);
32+
Document doc = col.find().first();
33+
System.out.println(doc.toJson());
34+
// end current-api-example
35+
client.close();
36+
}
37+
38+
private void example2() {
39+
// start current-api-mongoclientsettings-example
40+
MongoClientSettings options = MongoClientSettings.builder()
41+
.applyConnectionString(new ConnectionString(URI))
42+
.writeConcern(WriteConcern.W1).build();
43+
MongoClient client = MongoClients.create(options);
44+
// end current-api-mongoclientsettings-example
45+
client.close();
46+
}
47+
48+
49+
50+
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mycompany.app;
2+
3+
import com.mongodb.*;
4+
5+
public class LegacyAPI {
6+
7+
private static final String COLLECTION = "test";
8+
private static final String DATABASE = "test";
9+
private static final String URI = System.getenv("DRIVER_URL");
10+
11+
public static void main(String[] args) {
12+
LegacyAPI l = new LegacyAPI();
13+
l.example1();
14+
l.example2();
15+
}
16+
17+
private void example1() {
18+
// start legacy-api-example
19+
MongoClient client = new MongoClient(URI);
20+
DB db = client.getDB(DATABASE);
21+
DBCollection col = db.getCollection(COLLECTION);
22+
DBObject doc = col.find().one();
23+
System.out.println(doc.toString());
24+
// end legacy-api-example
25+
client.close();
26+
}
27+
28+
private void example2() {
29+
// start legacy-api-mongoclientoptions-example
30+
MongoClientURI mongoURI = new MongoClientURI(URI,
31+
MongoClientOptions.builder()
32+
.writeConcern(WriteConcern.W1));
33+
MongoClient client = new MongoClient(mongoURI);
34+
// end legacy-api-mongoclientoptions-example
35+
client.close();
36+
}
37+
38+
39+
}

source/includes/legacy-redirect.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. note:: Legacy API
2+
3+
If you are using the legacy API,
4+
:ref:`see our FAQ page <faq-legacy-connection>`
5+
to learn what changes you need to make to this code example.

source/usage-examples/bulkWrite.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ The output should look something like this:
8989
updated: 2
9090
deleted: 1
9191

92+
.. include:: /includes/legacy-redirect.rst
93+
9294
For additional information on the classes and methods mentioned on this
9395
page, see the following API documentation:
9496

source/usage-examples/command.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ following:
4343

4444
dbStats: {"db": "sample_mflix", "collections": 5, "views": 0, "objects": 75595, "avgObjSize": 692.1003770090614, "dataSize": 52319328, "storageSize": 29831168, "numExtents": 0, "indexes": 9, "indexSize": 14430208, "fileSize": 0, "nsSizeMB": 0, "ok": 1}
4545

46+
.. include:: /includes/legacy-redirect.rst
47+
4648
For additional information on the classes and methods mentioned on this
4749
page, see the following API documentation:
4850

source/usage-examples/count.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ like this (exact numbers may vary depending on your data):
7070
Estimated number of documents in the movies collection: 23541
7171
Number of movies from Spain: 755
7272

73+
.. include:: /includes/legacy-redirect.rst
74+
7375
For additional information on the classes and methods mentioned on this
7476
page, see the following API documentation:
7577

source/usage-examples/deleteMany.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,15 @@ sub-document.
3838
.. literalinclude:: /includes/usage-examples/code-snippets/DeleteMany.java
3939
:language: java
4040

41-
.. note::
42-
43-
If you are using the **legacy API**, see the :doc:`faq </faq>` to determine
44-
what changes you need to make to this code example.
45-
4641
When you run the example, you should see output that reports the number of
4742
documents deleted in your call to ``deleteMany()``.
4843

4944
.. code-block:: none
5045

5146
Deleted document count: 4
5247

48+
.. include:: /includes/legacy-redirect.rst
49+
5350
For additional information on the classes and methods mentioned on this
5451
page, see the following API documentation:
5552

source/usage-examples/deleteOne.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ filter to match movies with the ``title`` exactly matching the text
3131
.. literalinclude:: /includes/usage-examples/code-snippets/DeleteOne.java
3232
:language: java
3333

34-
.. note::
35-
36-
If you are using the **legacy API**, see the :doc:`FAQ </faq>` to determine
37-
what changes you need to make to this code example.
38-
3934
When you run the example, if the query filter you passed in your call to
4035
``deleteOne()`` matches a document and removes it, you should see output
4136
that looks something like this:
@@ -51,6 +46,8 @@ documents are removed and your call to ``deleteOne()`` returns the following:
5146

5247
Deleted document count: 0
5348

49+
.. include:: /includes/legacy-redirect.rst
50+
5451
For additional information on the classes and methods mentioned on this
5552
page, see the following API documentation:
5653

source/usage-examples/distinct.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ match movies that include "Carl Franklin" as one of the values in the
4949
.. literalinclude:: /includes/usage-examples/code-snippets/Distinct.java
5050
:language: java
5151

52-
.. note::
53-
54-
If you are using the **legacy API**, see the :doc:`FAQ </faq>` to determine
55-
what changes you need to make to this code example.
56-
5752
When you run the example, you should see output that reports each distinct
5853
year for all the movies that Carl Franklin was included as a director,
5954
which should look something like this:
@@ -66,6 +61,8 @@ which should look something like this:
6661
...
6762

6863

64+
.. include:: /includes/legacy-redirect.rst
65+
6966
For additional information on the classes and methods mentioned on this
7067
page, see the following API documentation:
7168

0 commit comments

Comments
 (0)