php-mogodb查找不工作


php mongodb find not working

已解决

为了查看数据,我在光标上做了一个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数据库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"
}

查找查询中缺少什么?有人能帮我吗?谢谢

光标对象在这里是一个"键"

$cursor = $collection->find(); find()方法将返回一个Cursor对象。现在您可以使用toArray()方法来获取数据。$dataArray = $cursor->toArray();就这么简单或者使用foreach逐个获取文档。Ps.FindOne(),返回一个数组。

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