How to query documents using “_id” field in Java mongodb driver without using the collection name? -


this document, want get.

{"_id": {"$oid": "5747f303631e1e261019914d"},  "school": "aaa", "name": "kamal", "likes": 200} 

i want passing _id without giving collection.

public dbobject finddocumentbyid(string id) {     basicdbobject query = new basicdbobject();     query.put("_id", new objectid(id));     dbobject dbobj = collection.findone(query);     return dbobj; } 

as searching different documents in different collections, don't want in collection _id belongs. without saying collection.findone(query).

how documents?

you must mention collection. 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         break;     }  } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -