Skip to content

Commit 8571df7

Browse files
authored
Merge pull request #604 from mcmorisi/DOCSP-45726-reduce-width
DOCSP-45726: Reduce width of Document code samples
2 parents 773bbf3 + 64a5012 commit 8571df7

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

source/fundamentals/data-formats/documents.txt

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ different field types:
116116

117117
Document author = new Document("_id", new ObjectId())
118118
.append("name", "Gabriel García Márquez")
119-
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
119+
.append("dateOfDeath",
120+
Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
120121
.append("novels", Arrays.asList(
121-
new Document("title", "One Hundred Years of Solitude").append("yearPublished", 1967),
122-
new Document("title", "Chronicle of a Death Foretold").append("yearPublished", 1981),
123-
new Document("title", "Love in the Time of Cholera").append("yearPublished", 1985)));
122+
new Document("title", "One Hundred Years of Solitude")
123+
.append("yearPublished", 1967),
124+
new Document("title", "Chronicle of a Death Foretold")
125+
.append("yearPublished", 1981),
126+
new Document("title", "Love in the Time of Cholera")
127+
.append("yearPublished", 1985)));
124128

125129
To insert this document into a collection, instantiate a collection
126130
using the ``getCollection()`` method and call the :doc:`insertOne
@@ -228,11 +232,16 @@ different field types:
228232
BsonDocument author = new BsonDocument()
229233
.append("_id", new BsonObjectId())
230234
.append("name", new BsonString("Gabriel García Márquez"))
231-
.append("dateOfDeath", new BsonDateTime(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()))
235+
.append("dateOfDeath",
236+
new BsonDateTime(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault())
237+
.toInstant().toEpochMilli()))
232238
.append("novels", new BsonArray(Arrays.asList(
233-
new BsonDocument().append("title", new BsonString("One Hundred Years of Solitude")).append("yearPublished", new BsonInt32(1967)),
234-
new BsonDocument().append("title", new BsonString("Chronicle of a Death Foretold")).append("yearPublished", new BsonInt32(1981)),
235-
new BsonDocument().append("title", new BsonString("Love in the Time of Cholera")).append("yearPublished", new BsonInt32(1985))
239+
new BsonDocument().append("title", new BsonString("One Hundred Years of Solitude"))
240+
.append("yearPublished", new BsonInt32(1967)),
241+
new BsonDocument().append("title", new BsonString("Chronicle of a Death Foretold"))
242+
.append("yearPublished", new BsonInt32(1981)),
243+
new BsonDocument().append("title", new BsonString("Love in the Time of Cholera"))
244+
.append("yearPublished", new BsonInt32(1985))
236245
)));
237246

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

362-
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}]}
371+
query result in extended json format: {"_id": {"$oid": "6035210f35bd203721c3eab8"},
372+
"name": "Gabriel García Márquez", "dateOfDeath": {"$date": "2014-04-17T04:00:00Z"},
373+
"novels": [{"title": "One Hundred Years of Solitude", "yearPublished": 1967},
374+
{"title": "Chronicle of a Death Foretold", "yearPublished": 1981},
375+
{"title": "Love in the Time of Cholera", "yearPublished": 1985}]}
363376

364377
.. tip::
365378

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

411-
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}]}
425+
query result in relaxed json format: {"_id": "6035210f35bd203721c3eab8", "name": "Gabriel García Márquez",
426+
"dateOfDeath": {"$date": "2014-04-17T04:00:00Z"}, "novels": [{"title": "One Hundred Years of Solitude",
427+
"yearPublished": 1967}, {"title": "Chronicle of a Death Foretold", "yearPublished": 1981},
428+
{"title": "Love in the Time of Cholera", "yearPublished": 1985}]}
412429

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

443460
BasicDBObject author = new BasicDBObject("_id", new ObjectId())
444461
.append("name", "Gabriel García Márquez")
445-
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17).atStartOfDay(ZoneId.systemDefault()).toInstant()))
462+
.append("dateOfDeath", Date.from(LocalDate.of(2014, 4, 17)
463+
.atStartOfDay(ZoneId.systemDefault()).toInstant()))
446464
.append("novels", Arrays.asList(
447-
new BasicDBObject("title", "One Hundred Years of Solitude").append("yearPublished", 1967),
448-
new BasicDBObject("title", "Chronicle of a Death Foretold").append("yearPublished", 1981),
449-
new BasicDBObject("title", "Love in the Time of Cholera").append("yearPublished", 1985)));
465+
new BasicDBObject("title", "One Hundred Years of Solitude")
466+
.append("yearPublished", 1967),
467+
new BasicDBObject("title", "Chronicle of a Death Foretold")
468+
.append("yearPublished", 1981),
469+
new BasicDBObject("title", "Love in the Time of Cholera")
470+
.append("yearPublished", 1985)));
450471

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

473-
BasicDBObject doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).first();
494+
BasicDBObject doc =
495+
collection.find(Filters.eq("name", "Gabriel García Márquez")).first();
474496
if (doc != null) {
475497
System.out.println("_id: " + doc.getObjectId("_id")
476498
+ ", name: " + doc.getString("name")

0 commit comments

Comments
 (0)