在mongoDB中查找具有ObjectID的文档

发布于 2024-11-29 22:10:26 字数 310 浏览 5 评论 0原文

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

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

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

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

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

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.

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安稳善良 2024-12-06 22:10:26

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

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

注释在 查询 页面上有点迟钝,但它确实提到...

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

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

白芷 2024-12-06 22:10:26

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

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

基于 Phil 的回答。

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.

清旖 2024-12-06 22:10:26

对于 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"; 
} 

出于测试目的:

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

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

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 />";
}
壹場煙雨 2024-12-06 22:10:26

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文