First page Back Continue Last page Image

MongoDB Example from Java

https://www.guru99.com/what-is-mongodb.html

MongoClient mongoClient = new MongoClient(mongoServer);

MongoDatabase database = mongoClient.getDatabase(“NGDBDemo”)

MongoCollection<Document> collection = database.getCollection(“test”);

Document people = new Document(); // A document for a person

people.put(“Name”, “Guy”);

people.put(“Email”, “guy@gmail.com”);

BasicDBList friendList = new BasicDBList(); // List of friends

BasicDBObject friendDoc = new BasicDBObject(); // A Single Friend

friendDoc.put(“Name”, “Jo”);

friendDoc.put(“Email”, “Jo@gmail.com”);

friendList.add(friendDoc); // Add a Friend

friendDoc.clear();

friendDoc.put(“Name”, “John”);

friendDoc.put(“Email”, “john@gmail.comJo@gmail.com”);

friendList.add(friendDoc);

people.put(“Friends”, friendDoc);

collection.insetOne(people);