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

后端 未结 12 2600
心在旅途
心在旅途 2020-11-28 01:58

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(\"4ecbe7f9e8c

12条回答
  •  情歌与酒
    2020-11-28 02:25

    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 }
    

提交回复
热议问题