diff --git a/source/fundamentals/data-formats/documents.txt b/source/fundamentals/data-formats/documents.txt index 5fe6a98aa..58e9146e9 100644 --- a/source/fundamentals/data-formats/documents.txt +++ b/source/fundamentals/data-formats/documents.txt @@ -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 @@ -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 @@ -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:: @@ -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\"}"); @@ -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: @@ -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 @@ -470,7 +491,8 @@ data from the collection using the following code: import com.mongodb.client.model.Filters; // - 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")