Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query documents using "_id" field in Java mongodb driver?

I am trying to find documents in MongoDB by searching on "_id" key. My document looks like this-

{   "_id" : ObjectId("4f693d40e4b04cde19f17205"),   "hostname" : "hostnameGoesHere",   "OSType" : "OSTypeGoesHere" } 

I am trying to search this document as-

ObjectId id= new ObjectId("4f693d40e4b04cde19f17205");         BasicDBObject obj = new BasicDBObject();         obj.append("_id", id);         BasicDBObject query = new BasicDBObject();         query.putAll(query); 

But I get below error-

error: reference to putAll is ambiguous, both method putAll(Map) in BasicBSONObject and method putAll(BSONObject) in BasicBSONObject match         query.putAll(query); 

The append method of BasicDBObject supports (String Key, Value) and if I pass "_id" as String to this method, no documents are matched.

So my question is how do I pass "_id"?

like image 230
user837208 Avatar asked Mar 21 '12 02:03

user837208


People also ask

How does the value of _id get assigned to a document MongoDB?

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. This also applies to documents inserted through update operations with upsert: true.

Can we set _id in MongoDB?

_id field is reserved for primary key in mongodb, and that should be a unique value. If you don't set anything to _id it will automatically fill it with "MongoDB Id Object". But you can put any unique info into that field.

What is the type of the _id field if it's generated by MongoDB automatically?

Architecturally, by default the _id field is an ObjectID, one of MongoDB's BSON types. The ObjectID is the primary key for the stored document and is automatically generated when creating a new document in a collection.

Why does MongoDB use _id?

In MongoDB, _id field as the primary key for the collection so that each document can be uniquely identified in the collection. The _id field contains a unique ObjectID value. When you query the documents in a collection, you can see the ObjectId for each document in the collection.


2 Answers

Not sure if others might be searching for answers on this topic, but here is the easiest way to search for a MongoDB record based on "_id". The MongoDB documentation is not updated and still shows ObjectId as being part of the com.mongodb package (it also generally does not give a lot of information on searching by ObjectId).

import org.bson.types.ObjectId;  public DBObject findDocumentById(String id) {      BasicDBObject query = new BasicDBObject();     query.put("_id", new ObjectId(id));      DBObject dbObj = collection.findOne(query);     return dbObj; } 
like image 126
Chris Avatar answered Oct 09 '22 04:10

Chris


For those who are seeking a more up to date method, especially with 3.4:

import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId;  import static com.mongodb.client.model.Filters.eq;  //...... MongoCollection<Document> myCollection = database.getCollection("myCollection"); Document document = myCollection.find(eq("_id", new ObjectId("4f693d40e4b04cde19f17205"))).first(); if (document == null) {     //Document does not exist } else {     //We found the document } 
like image 30
bogolyandras Avatar answered Oct 09 '22 03:10

bogolyandras