Is there a findById shortcut for the MongoDB shell?

The most common thing I do in the mongo shell shell is to search for objects by ID, for example:

db.collection.find({_id: ObjectId("55a3e051dc75954f0f37c2f2"})

I do this over and over and I have to wrap the identifier over and over again with the ObjectId. I am sorry that I did not have an-- findByIdshaped abbreviated form, like what the mongoose provides. I feel that the shell should be smart enough to understand what I mean here, for example:

db.collection.find("55a3e051dc75954f0f37c2f2")

How can i do this? Or are there alternative ways to request id in mongo shell?

+4
source share
1 answer

, , , ~/.mongorc.js, , mongo:

DBCollection.prototype.findById = function(id) {
    return db.getCollection(this._shortName).find( { "_id" : ObjectId(id) } );
}

- db.collection.findById("55a3e051dc75954f0f37c2f2")

+4

Source: https://habr.com/ru/post/1598069/


All Articles