Java & MongoDB Tutorial
Java & MongoDB Useful Resources
Selected Reading
- Java & MongoDB - Sorting Records
- Java & MongoDB - Limiting Records
- Java & MongoDB - Referenced Documents
- Java & MongoDB - Embedded Documents
- Java & MongoDB - Delete Document
- Java & MongoDB - Update Document
- Java & MongoDB - Select Document
- Java & MongoDB - Insert Document
- Java & MongoDB - Display Collections
- Java & MongoDB - Drop Collection
- Java & MongoDB - Create Collection
- Java & MongoDB - Drop Database
- Java & MongoDB - Show Databases
- Java & MongoDB - Connect Database
- Java & MongoDB - Environment Setup
- Java & MongoDB - Overview
- Java & MongoDB - Home
Java & MongoDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java & MongoDB - Referenced Documents
Java & MongoDB - Referenced Documents
To insert a document with referenced documents in a collection, you can use DBRef object as shown below −
// create a document Document comment1 = new Document(); comment1.put("_id", "comment1"); comment1.put("user", "User1"); comment1.put("message", "My First Comment"); comment1.put("dateCreated", "20/2/2020"); comment1.put("pke", "0"); // create a database reference DBRef comment1Ref = new DBRef("post", comment1.get("_id")); // insert the reference in the document Document document = new Document("title", "Java Overview") .append("comment1", comment1Ref);
Example
Following is the code snippet to insert a document with referenced documents and display them −
import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.DBRef; 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"); MongoCollection<Document> collection = database.getCollection("post"); List<String> tags = new ArrayList<String>(); tags.add("mongodb"); tags.add("database"); tags.add("NoSQL"); Document comment1 = new Document(); comment1.put("_id", "comment1"); comment1.put("user", "User1"); comment1.put("message", "My First Comment"); comment1.put("dateCreated", "20/2/2020"); comment1.put("pke", "0"); DBRef comment1Ref = new DBRef("post", comment1.get("_id")); Document comment2 = new Document(); comment2.put("_id", "comment2"); comment2.put("user", "User2"); comment2.put("message", "My Second Comment"); comment2.put("dateCreated", "20/2/2020"); comment2.put("pke", "0"); DBRef comment2Ref = new DBRef("post", comment2.get("_id")); List<Document> comments = new ArrayList<Document>(); comments.add(comment1); comments.add(comment2); Document document = new Document("title", "Java Overview") .append("description", "Java is programming language") .append("by", "tutorials point") .append("url", "http://www.tutorialspoint.com") .append("tags",tags) .append("comment1", comment1Ref) .append("comment2", comment2Ref); collection.insertMany(comments); collection.insertOne(document); FindIterable<Document> documents = collection.find(Filters.eq("title","Java 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
Output
On executing, the above program gives you the following output.
Document{{_id=60b7b26b7671f469993fdc3c, title=Java Overview, description=Java is programming language, by=tutorials point, url=http://www.tutorialspoint.com, tags=[mongodb, database, NoSQL], comment1={ "$ref" : "post", "$id" : "comment1" }, comment2={ "$ref" : "post", "$id" : "comment2" } }}Advertisements