Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Web Technology by (47.6k points)

I've found this question answered for C# and Perl, but not in the native interface. I thought this would work:

db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )

The query returned no results. I found the 4ecbe7f9e8c1c9092c000027 by doing db.theColl.find()and grabbing an ObjectId. There are several thousand objects in that collection.

I've read all the pages that I could find on the website and didn't find it. Is this just a strange thing to do? It seems pretty normal to me.

2 Answers

0 votes
by (106k points)

You can use the below-mentioned code to search for an object by its ObjectId in the console:-

> db.test.insert({x: 1}) 

> db.test.find() 

{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 } 

> db.test.find({"_id" : ObjectId("4ecc05e55dd98a436ddcc47c")}) 

{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 } 

> db.test.find(ObjectId("4ecc05e55dd98a436ddcc47c")) 

{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }

0 votes
by (108k points)

If you're using Node.js, you can apply the following command:

> var ObjectId = require('mongodb').ObjectId; 

> var id = req.params.gonderi_id;       

> var o_id = new ObjectId(id);

> db.test.find({_id:o_id})

Related questions

Browse Categories