How do I search for an object by its ObjectId in the mongo console?

Mongodb

Mongodb Problem Overview


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 mongodb.org website and didn't find it. Is this just a strange thing to do? It seems pretty normal to me.

Mongodb Solutions


Solution 1 - Mongodb

Not strange at all, people do this all the time. Make sure the collection name is correct (case matters) and that the ObjectId is exact.

Documentation is here

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

> db.test.find()                                               // no criteria
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }      

> db.test.find({"_id" : ObjectId("4ecc05e55dd98a436ddcc47c")}) // explicit
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }

> db.test.find(ObjectId("4ecc05e55dd98a436ddcc47c"))           // shortcut
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }

Solution 2 - Mongodb

If you're using Node.js:

var ObjectId = require('mongodb').ObjectId; 
var id = req.params.gonderi_id;       
var o_id = new ObjectId(id);
db.test.find({_id:o_id})

Edit: corrected to new ObjectId(id), not new ObjectID(id)

Solution 3 - Mongodb

Even easier, especially with tab completion:

db.test.find(ObjectId('4ecc05e55dd98a436ddcc47c'))

Edit: also works with the findOne command for prettier output.

Solution 4 - Mongodb

You Have missed to insert Double Quotes. The Exact Query is

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

Solution 5 - Mongodb

If you are working on the mongo shell, Please refer this : Answer from Tyler Brock

I wrote the answer if you are using mongodb using node.js

You don't need to convert the id into an ObjectId. Just use :

db.collection.findById('4ecbe7f9e8c1c9092c000027');

this collection method will automatically convert id into ObjectId.

On the other hand :

db.collection.findOne({"_id":'4ecbe7f9e8c1c9092c000027'}) doesn't work as expected. You've manually convert id into ObjectId.

That can be done like this :

let id = '58c85d1b7932a14c7a0a320d';

let o_id = new ObjectId(id);   // id as a string is passed

db.collection.findOne({"_id":o_id});

Solution 6 - Mongodb

I think you better write something like this:

db.getCollection('Blog').find({"_id":ObjectId("58f6724e97990e9de4f17c23")})

Solution 7 - Mongodb

I just had this issue and was doing exactly as was documented and it still was not working.

Look at your error message and make sure you do not have any special characters copied in. I was getting the error

SyntaxError: illegal character @(shell):1:43

When I went to character 43 it was just the start of my object ID, after the open quotes, exactly as I pasted it in. I put my cursor there and hit backspace nothing appeared to happen when it should have removed the open quote. I hit backspace again and it removed the open quote, then I put the quote back in and executed the query and it worked, despite looking exactly the same.

I was doing development in WebMatrix and copied the object id from the console. Whenever you copy from the console in WebMatrix you're likely to pick up some invisible characters that will cause errors.

Solution 8 - Mongodb

Once you opened the mongo CLI, connected and authorized on the right database.

> The following example shows how to [find the document][1] with the _id=568c28fffc4be30d44d0398e from a collection called “products”:

db.products.find({"_id": ObjectId("568c28fffc4be30d44d0398e")})

[1]: https://specify.io/how-tos/find-documents-in-mongodb-using-the-mongo-shell "mongodb find by id"

Solution 9 - Mongodb

In MongoDB Stitch functions it can be done using BSON like below:

Use the ObjectId helper in the BSON utility package for this purpose like in the follwing example:

var id = "5bb9e9f84186b222c8901149";  
BSON.ObjectId(id);

Solution 10 - Mongodb

To use Objectid method you don't need to import it. It is already on the mongodb object.

var ObjectId = new db.ObjectId('58c85d1b7932a14c7a0a320d');
db.yourCollection.findOne({ _id: ObjectId }, function (err, info) {
   console.log(info)
});
               

Solution 11 - Mongodb

Simply do:

db.getCollection('test').find('4ecbe7f9e8c1c9092c000027');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjcollumView Question on Stackoverflow
Solution 1 - MongodbTyler BrockView Answer on Stackoverflow
Solution 2 - MongodbMustafa DenizView Answer on Stackoverflow
Solution 3 - MongodbMPlanchardView Answer on Stackoverflow
Solution 4 - MongodbMohamed Abdullah JView Answer on Stackoverflow
Solution 5 - MongodbSaurabhView Answer on Stackoverflow
Solution 6 - Mongodbmina_anwerView Answer on Stackoverflow
Solution 7 - MongodbPatrick GrahamView Answer on Stackoverflow
Solution 8 - MongodbOliver WolfView Answer on Stackoverflow
Solution 9 - MongodbKushal BhalaikView Answer on Stackoverflow
Solution 10 - MongodbMiguel PegueroView Answer on Stackoverflow
Solution 11 - MongodbShalabh RaizadaView Answer on Stackoverflow