Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-45726: Reduce width of Document code samples #604

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions source/fundamentals/data-formats/documents.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,15 @@ different field types:

Document author = new Document("_id", new ObjectId())
.append("name", "Gabriel García Márquez")
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
.append("dateOfDeath",
Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
.append("novels", Arrays.asList(
new Document("title", "One Hundred Years of Solitude").append("yearPublished", 1967),
new Document("title", "Chronicle of a Death Foretold").append("yearPublished", 1981),
new Document("title", "Love in the Time of Cholera").append("yearPublished", 1985)));
new Document("title", "One Hundred Years of Solitude")
.append("yearPublished", 1967),
new Document("title", "Chronicle of a Death Foretold")
.append("yearPublished", 1981),
new Document("title", "Love in the Time of Cholera")
.append("yearPublished", 1985)));

To insert this document into a collection, instantiate a collection
using the ``getCollection()`` method and call the :doc:`insertOne
Expand Down Expand Up @@ -228,11 +232,16 @@ different field types:
BsonDocument author = new BsonDocument()
.append("_id", new BsonObjectId())
.append("name", new BsonString("Gabriel García Márquez"))
.append("dateOfDeath", new BsonDateTime(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()))
.append("dateOfDeath",
new BsonDateTime(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault())
.toInstant().toEpochMilli()))
.append("novels", new BsonArray(Arrays.asList(
new BsonDocument().append("title", new BsonString("One Hundred Years of Solitude")).append("yearPublished", new BsonInt32(1967)),
new BsonDocument().append("title", new BsonString("Chronicle of a Death Foretold")).append("yearPublished", new BsonInt32(1981)),
new BsonDocument().append("title", new BsonString("Love in the Time of Cholera")).append("yearPublished", new BsonInt32(1985))
new BsonDocument().append("title", new BsonString("One Hundred Years of Solitude"))
.append("yearPublished", new BsonInt32(1967)),
new BsonDocument().append("title", new BsonString("Chronicle of a Death Foretold"))
.append("yearPublished", new BsonInt32(1981)),
new BsonDocument().append("title", new BsonString("Love in the Time of Cholera"))
.append("yearPublished", new BsonInt32(1985))
)));

To insert this document into a collection, instantiate a collection
Expand Down Expand Up @@ -359,7 +368,11 @@ The output of the preceding code resembles the following:
.. code-block:: none
:copyable: false

query result in extended json format: {"_id": {"$oid": "6035210f35bd203721c3eab8"}, "name": "Gabriel García Márquez", "dateOfDeath": {"$date": "2014-04-17T04:00:00Z"}, "novels": [{"title": "One Hundred Years of Solitude", "yearPublished": 1967}, {"title": "Chronicle of a Death Foretold", "yearPublished": 1981}, {"title": "Love in the Time of Cholera", "yearPublished": 1985}]}
query result in extended json format: {"_id": {"$oid": "6035210f35bd203721c3eab8"},
"name": "Gabriel García Márquez", "dateOfDeath": {"$date": "2014-04-17T04:00:00Z"},
"novels": [{"title": "One Hundred Years of Solitude", "yearPublished": 1967},
{"title": "Chronicle of a Death Foretold", "yearPublished": 1981},
{"title": "Love in the Time of Cholera", "yearPublished": 1985}]}

.. tip::

Expand Down Expand Up @@ -394,7 +407,8 @@ The output of the preceding code resembles the following:
JsonObject author = new JsonObject("{\"_id\": \"6035210f35bd203721c3eab8\", "
+ "\"name\": \"Gabriel García Márquez\", "
+ "\"dateOfDeath\": {\"$date\": \"2014-04-17T04:00:00Z\"}, "
+ "\"novels\": [{\"title\": \"One Hundred Years of Solitude\", \"yearPublished\": 1967}, {\"title\": \"Chronicle of a Death Foretold\", \"yearPublished\": 1981}, "
+ "\"novels\": [{\"title\": \"One Hundred Years of Solitude\", \"yearPublished\": 1967},
{\"title\": \"Chronicle of a Death Foretold\", \"yearPublished\": 1981}, "
+ "{\"title\": \"Love in the Time of Cholera\", \"yearPublished\": 1985}]}\n");
collection.insertOne(author);
JsonObject query = new JsonObject("{\"name\": \"Gabriel Garc\\u00eda M\\u00e1rquez\"}");
Expand All @@ -408,7 +422,10 @@ The output of the preceding code resembles the following:
.. code-block:: none
:copyable: false

query result in relaxed json format: {"_id": "6035210f35bd203721c3eab8", "name": "Gabriel García Márquez", "dateOfDeath": {"$date": "2014-04-17T04:00:00Z"}, "novels": [{"title": "One Hundred Years of Solitude", "yearPublished": 1967}, {"title": "Chronicle of a Death Foretold", "yearPublished": 1981}, {"title": "Love in the Time of Cholera", "yearPublished": 1985}]}
query result in relaxed json format: {"_id": "6035210f35bd203721c3eab8", "name": "Gabriel García Márquez",
"dateOfDeath": {"$date": "2014-04-17T04:00:00Z"}, "novels": [{"title": "One Hundred Years of Solitude",
"yearPublished": 1967}, {"title": "Chronicle of a Death Foretold", "yearPublished": 1981},
{"title": "Love in the Time of Cholera", "yearPublished": 1985}]}

For more information about the methods and classes mentioned in this section,
see the following API Documentation:
Expand Down Expand Up @@ -442,11 +459,15 @@ different field types:

BasicDBObject author = new BasicDBObject("_id", new ObjectId())
.append("name", "Gabriel García Márquez")
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17)
.atStartOfDay(ZoneId.systemDefault()).toInstant()))
.append("novels", Arrays.asList(
new BasicDBObject("title", "One Hundred Years of Solitude").append("yearPublished", 1967),
new BasicDBObject("title", "Chronicle of a Death Foretold").append("yearPublished", 1981),
new BasicDBObject("title", "Love in the Time of Cholera").append("yearPublished", 1985)));
new BasicDBObject("title", "One Hundred Years of Solitude")
.append("yearPublished", 1967),
new BasicDBObject("title", "Chronicle of a Death Foretold")
.append("yearPublished", 1981),
new BasicDBObject("title", "Love in the Time of Cholera")
.append("yearPublished", 1985)));

To insert this document into a collection, instantiate a collection using
the ``getCollection()`` method specifying the ``BasicDBObject`` class as
Expand All @@ -470,7 +491,8 @@ data from the collection using the following code:
import com.mongodb.client.model.Filters;
// <MongoCollection setup code here>

BasicDBObject doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).first();
BasicDBObject doc =
collection.find(Filters.eq("name", "Gabriel García Márquez")).first();
if (doc != null) {
System.out.println("_id: " + doc.getObjectId("_id")
+ ", name: " + doc.getString("name")
Expand Down