[英]php mongodb find not working
SOLVED 解决了
To view the data, I was doing a var_dump on the cursor and you have to loop through the cursor first to var_dump it. 要查看数据,我在光标上执行var_dump,您必须先将光标循环到var_dump它。
foreach($user_images as $image) {
var_dump($image)
}
Can find out more about this at: 可以在以下位置找到更多相关信息:
http://php.net/manual/en/class.mongocursor.php http://php.net/manual/en/class.mongocursor.php
/SOLVED /解决了
I have a collection called 'user_image' in my MongoDB. 我的MongoDB中有一个名为'user_image'的集合。 I am using PHP 5.3 with mongoDB db v2.0.5, pdfile version 4.5.
我使用PHP 5.3和mongoDB db v2.0.5,pdfile版本4.5。 I have this setup in my XAMPP.
我在XAMPP中有这个设置。 I am simply trying to find all documents in the collection.
我只是想找到集合中的所有文档。 When I run the information below, nothing returns back even though I can confirm in the terminal running the db.user_image.find() that it returns results.
当我运行下面的信息时,即使我可以在运行db.user_image.find()的终端中确认它返回结果,也没有返回任何内容。
$m = new Mongo();
$db = $m->selectDB('dev_app');
$collection = new MongoCollection($db, 'user_image');
$collection->find();
If I change the query to simply use findOne by a user_uuid I get a result! 如果我更改查询只是通过user_uuid使用findOne我得到一个结果! Example below:
示例如下:
$collection->findOne(array('user_uuid' => 'de977803-f198-416a-8806-acbc1fa3f718'));
Here is an example document in the collection user_image: 这是集合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"
}
What am I missing in the find query? 我在查询查询中缺少什么? Can anyone help me?
谁能帮我? Thanks.
谢谢。
The $cursor = $collection->find();
$cursor = $collection->find();
find() method will return a Cursor Object. find()方法将返回一个Cursor对象。 Now you can use toArray() method to get your data.
现在您可以使用toArray()方法来获取数据。
$dataArray = $cursor->toArray();
Simple as that. 就那么简单。 Or use foreach to get documents one by one .
或者使用foreach逐个获取文档 。 Ps.
PS。 FindOne() which returns an array.
FindOne()返回一个数组。
https://www.php.net/manual/en/class.mongodb-driver-cursor.php https://www.php.net/manual/en/class.mongodb-driver-cursor.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.