1

Using the java MongoClient library, how can I find a document in a collection and only return specific Objects? I know this is possible for 1 Object but not sure about multiple.

For 1 Object:

DBCursor cursor = db.getCollection(collectionName).find(dbObject)

Possibly for 2 Objects??:

DBCursor cursor = db.getCollection(collectionName).find(dbObject1, dbObject2, dbObject3)
Andy Jenkins
  • 617
  • 8
  • 26

1 Answers1

0

Probably your question finds an answer in this poll: find in MongoCollection<Document>

Anyway, for one object:

import static com.mongodb.client.model.Filters.*;
 MongoClient client = new MongoClient();
 MongoDatabase database = client.getDatabase("mydb");
 MongoCollection<Document> collection = database.getCollection("mycoll");
 myDoc = collection.find(eq("_id", "42")).first(); //finds object with _id equals to 42

For finding many objects there can be many solutions, it is hard to reply without knowing better your data. I would check this out: http://www.thejavageek.com/2015/08/24/retrieve-documents-from-mongodb-using-java/ or the official documentation as well https://resources.mongodb.com/getting-started-with-mongodb?_ga=2.26976315.702528013.1555420535-809832624.1541599802

Good luck!

Mazzespazze
  • 111
  • 12