Here is a quick reference guide to MongoDB Java Driver version 3.0
How to connect to a local Database and get an handle to DB named “mydb”
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("mydb");
How to get an handle to the Collection named “test”
MongoCollection<Document> collection = database.getCollection("test");
How to create a Document and insert it:
Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc);
How to convert a Document into JSON:
String json = myDoc.toJson();
How to find the first Document in a Collection
Document myDoc = collection.find().first();
How to insert multiple Documents in one shot:
List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents);
How to count the number of Documents in a Collection:
System.out.println("total # of documents " + collection.count());
How to iterate over a Collection
MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); }
How to find one Document whose key matches with a value:
Document myDoc = collection.find(eq("i", 71)).first();
How to find all Documents which are matching a constraint:
MongoCursor<Document> cursor = collection.find(gt("i", 50)).iterator();
How to find all Documents which are matching multiple constraints:
MongoCursor<Document> cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator();
How to apply a filter which executes a Callback method defined in a Block class:
Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock);
How to use a Projection:
Document myDoc = collection.find().projection(excludeId()).first();
How to update a Document:
collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110)));
How to update multiple Documents:
UpdateResult updateResult = collection.updateMany(lt("i", 100), new Document("$inc", new Document("i", 100))); System.out.println(updateResult.getModifiedCount());
How to delete a Document
collection.deleteOne(eq("i", 110));
How to delete multiple Documents:
DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount());
How to perform a Bulk write:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add(new InsertOneModel<Document>(new Document("_id", 4))); writes.add(new InsertOneModel<Document>(new Document("_id", 5))); writes.add(new InsertOneModel<Document>(new Document("_id", 6))); writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2)))); writes.add(new DeleteOneModel<Document>(new Document("_id", 2))); writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4))); collection.bulkWrite(writes);