Reputation: 113
This is my document, that i want to get.
{"_id": {"$oid": "5747f303631e1e261019914d"},
"school": "aaa", "name": "Kamal", "likes": 200}
I want to get this only by passing its _id
without giving its collection.
public DBObject findDocumentById(String id) {
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
DBObject dbObj = collection.findOne(query);
return dbObj;
}
As I am searching for different documents in different collections, I don't want to say in which collection does the _id
belongs. So without saying collection.findOne(query)
.
How to get the documents?
Upvotes: 1
Views: 3067
Reputation: 387
You must mention what is the Collection. But you can try this.
for(String collectionName : mongoOperation.getCollectionNames()){
DBCollection collection = mongoOperation.getCollection(collectionName);
DBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
DBCursor cursor = dbCollection.find(query);
if(cursor.hasNext()){
//match
//do something
break;
}
}
Upvotes: 2