Find a document with ObjectID in mongoDB

前端 未结 4 1704
暗喜
暗喜 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 09:57

    I think now the API changes to MongoDB\BSON\ObjectID, also you can use [] to denote an array in PHP 5.4+, so it should be:

    $item = $collection->findOne(['_id' => new MongoDB\BSON\ObjectID( idToken )]);
    

    based on Phil's answer.

提交回复
热议问题