find in MongoCollection<Document>
Try to create a filter to pass to the find()
method to get a subset of the documents in your collection. For example, to find the document for which the value of the _id
field is test
, you would do the following:
import static com.mongodb.client.model.Filters.*;
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycoll");
Document myDoc = collection.find(eq("_id", "test")).first();
System.out.println(myDoc.toJson());