3

解决了

为了查看数据,我在游标上做了一个 var_dump,你必须先遍历游标来 var_dump 它。

foreach($user_images as $image) {
     var_dump($image)
}

可以在以下位置了解更多信息:

http://php.net/manual/en/class.mongocursor.php

/解决了

我的 MongoDB 中有一个名为“user_image”的集合。我正在使用 PHP 5.3 和 mongoDB db v2.0.5,pdfile 版本 4.5。我的 XAMPP 中有这个设置。我只是想找到集合中的所有文档。当我运行下面的信息时,即使我可以在运行 db.user_image.find() 的终端中确认它返回结果,也没有任何返回。

$m = new Mongo();
$db = $m->selectDB('dev_app');
$collection = new MongoCollection($db, 'user_image');
$collection->find();

如果我将查询更改为通过 user_uuid 简单地使用 findOne,我会得到一个结果!下面的例子:

$collection->findOne(array('user_uuid' => 'de977803-f198-416a-8806-acbc1fa3f718'));

这是集合 user_image 中的示例文档:

{
    "_id" : ObjectId("500c3f13ab8692ced0d9df6f"),
    "user_uuid" : "de977803-f198-416a-8806-acbc1fa3f718",
    "image_name" : "4a5e286e101429da0a3c3a576ffa4878.jpg",
    "image_url" : "/uploaded_files/files/4a5e286e101429da0a3c3a576ffa4878.jpg",
    "sm_thumb_url" : "/uploaded_files/thumbnails/4a5e286e101429da0a3c3a576ffa4878.jpg",
    "md_thumb_url" : "/uploaded_files/files/4a5e286e101429da0a3c3a576ffa4878.jpg",
    "lg_thumb_url" : "/uploaded_files/files/4a5e286e101429da0a3c3a576ffa4878.jpg",
    "status" : "A",
    "created" : ISODate("2012-07-22T17:57:36.835Z"),
    "modified" : ISODate("2012-07-22T17:57:36.835Z"),
    "created_by_uuid" : "de977803-f198-416a-8806-acbc1fa3f718",
    "modified_by_uuid" : "de977803-f198-416a-8806-acbc1fa3f718"
}

我在查找查询中缺少什么?谁能帮我?谢谢。

4

1 回答 1

0

光标对象是这里的“关键”

find() 方法将$cursor = $collection->find();返回一个光标对象。现在您可以使用 toArray() 方法来获取数据。 $dataArray = $cursor->toArray();就那么简单。或者使用foreach一个一个的获取文档。附言。FindOne() 返回一个数组。

https://www.php.net/manual/en/class.mongodb-driver-cursor.php

于 2019-07-26T01:53:37.903 回答