English 中文(简体)
Java & MongoDB - Embedded Documents
  • 时间:2024-12-22

Java & MongoDB - Embedded Documents


Previous Page Next Page  

To insert a document with embedded document in a collection, you can use DBObject/BasicDBObject objects as shown below −


// Create an embedded document
BasicDBObject comment = new BasicDBObject();
comment.put("user", "User1");
comment.put("message", "My First Comment");
comment.put("dateCreated", "20/2/2020");
comment.put("pke", "0");

// create an array
List<String> tags = new ArrayList<String>();
tags.add("mongodb");
tags.add("database");
tags.add("NoSQL");


// add array and embedded documents
Document document = new Document("title", "MongoDB Overview")
   .append("tags",tags)
   .append("comment", comment);

Example

Following is the code snippet to insert a document with embedded documents and display them −


import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.cpent.FindIterable;
import com.mongodb.cpent.MongoCpent;
import com.mongodb.cpent.MongoCpents;
import com.mongodb.cpent.MongoCollection;
import com.mongodb.cpent.MongoDatabase;
import com.mongodb.cpent.model.Filters;

pubpc class Tester {
   pubpc static void main(String[] args) {
      // Creating a Mongo cpent 
      MongoCpent mongoCpent = MongoCpents.create("mongodb://localhost:27017");
      MongoDatabase database = mongoCpent.getDatabase("myDb");

      // Create the collection
      database.createCollection("post");

      MongoCollection<Document> collection = database.getCollection("post");
      List<String> tags = new ArrayList<String>();
      tags.add("mongodb");
      tags.add("database");
      tags.add("NoSQL");

      BasicDBObject comment1 = new BasicDBObject();
      comment1.put("user", "User1");
      comment1.put("message", "My First Comment");
      comment1.put("dateCreated", "20/2/2020");
      comment1.put("pke", "0");

      BasicDBObject comment2 = new BasicDBObject();
      comment2.put("user", "User2");
      comment2.put("message", "My Second Comment");
      comment2.put("dateCreated", "20/2/2020");
      comment2.put("pke", "0");

      List<DBObject> comments = new ArrayList<DBObject>();
      comments.add(comment1);
      comments.add(comment2);

      Document document = new Document("title", "MongoDB Overview")
         .append("description", "MongoDB is no SQL database")
         .append("by", "tutorials point")
         .append("url", "http://www.tutorialspoint.com")
         .append("tags",tags)
         .append("comments", comments);

      collection.insertOne(document);

      FindIterable<Document> documents = collection.find(Filters.eq("title","MongoDB Overview"));

      for (Document doc : documents) {
         System.out.println(doc);
      }
   }
}

Now, let s compile and run the above program as shown below.


$javac Tester.java 
$java Tester

On executing, the above program gives you the following output.

Output


Document{{_id=60b7ab7614bd6b4a14b46d47, title=MongoDB Overview, 
   description=MongoDB is no SQL database, by=tutorials point, 
   url=http://www.tutorialspoint.com, tags=[mongodb, database, NoSQL], 
   comments=[Document{{user=User1, message=My First Comment, 
   dateCreated=20/2/2020, pke=0}}, Document{{user=User2, message=My Second Comment, 
   dateCreated=20/2/2020, pke=0}}]
}}
Advertisements