php 在 mongoDB 中查找带有 ObjectID 的文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7074309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:57:42  来源:igfitidea点击:

Find a document with ObjectID in mongoDB

phpmongodbmongodb-php

提问by jwchang

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

当我将一些文档插入一个集合(没有 ObjectID)时,mongoDB 添加了它自己的 ObjectID。

I want to query a document by its unique ObjectID.

我想通过其唯一的 ObjectID 查询文档。

$db->collection_name->find(array('_id'=>'4e49fd8269fd873c0a000000')));

It does not work either with prefix of MongoID or ObjectID in front of '4e49fd8269fd873c0a000000'.

它不适用于 '4e49fd8269fd873c0a000000' 前面的 MongoID 或 ObjectID 前缀。

What is the proper way to query by ObjectID with mongoDB in PHP?

在 PHP 中使用 mongoDB 通过 ObjectID 查询的正确方法是什么?

回答by Phil

Pretty sure you have to use a MongoIdobject, eg

很确定你必须使用一个MongoId对象,例如

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

The notes on the Queryingpage 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

除非用户另外指定,否则 _id 字段是一个 MongoId。最常见的错误是试图使用字符串来匹配 MongoId。请记住,这是两种不同的数据类型,并且不会以字符串“array()”与空数组不同的方式相互匹配

回答by coolgod

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:

我认为现在 API 更改为MongoDB\BSON\ObjectID,您也可以[]在 PHP 5.4+ 中使用它来表示数组,所以它应该是:

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

based on Phil's answer.

基于菲尔的回答。

回答by Roman

For MongoDB\Driver\Manager, a modern version of a MongoDB, you might consider the following working code:

对于 MongoDB\Driver\Manager(MongoDB 的现代版本),您可以考虑以下工作代码:

try {
  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );

  $filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
  $options = [];

  $query = new MongoDB\Driver\Query($filter, $options);     
  $docs = $manager->executeQuery('YourDbName.YourCollectionName', $query);
}
catch (MongoDB\Driver\Exception\Exception $e) { 
  $filename = basename(__FILE__); 
  echo "The $filename script has experienced an error.\n"; 
  echo "It failed with the following exception:\n"; 
  echo "Exception:", $e->getMessage(), "\n"; 
} 

For testing purposes:

出于测试目的:

foreach ($docs as $doc) {
  print_r($doc);
  //or you can: echo "$doc->item  $row->qty  $row->status<br />";
}

回答by Elan Ruusam?e

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

使用alcaeus/mongo-php-adapter(在 php 7 下),需要转换\MongoId为 BSON 类型:

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