Find a document with ObjectID in mongoDB

前端 未结 4 1697
暗喜
暗喜 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:55

    With alcaeus/mongo-php-adapter (under php 7), needed to convert \MongoId to BSON type:

    $filter = [];
    $filter['_id'] = (new \MongoId('4e49fd8269fd873c0a000000'))->toBSONType();
    $cursor = $collection->find($filter);
    

提交回复
热议问题