Find a document with ObjectID in mongoDB

前端 未结 4 1699
暗喜
暗喜 2020-12-13 09:32

When I inserted some documents into a collection (without an ObjectID) mongoDB adds its own ObjectIDs.

I want to query a document by its unique ObjectID.

<         


        
4条回答
  •  一个人的身影
    2020-12-13 10:12

    Pretty sure you have to use a MongoId object, eg

    $item = $collection->findOne(array(
        '_id' => new MongoId('4e49fd8269fd873c0a000000')));
    

    The notes on the Querying page are a little obtuse but it does mention...

    Unless the user has specified otherwise, the _id field is a MongoId. The most common mistake is attepting to use a string to match a MongoId. Keep in mind that these are two different datatypes, and will not match each other in the same way that the string "array()" is not the same as an empty array

提交回复
热议问题