简体   繁体   中英

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.

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

Can find out more about this at:

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

/SOLVED

I have a collection called 'user_image' in my MongoDB. I am using PHP 5.3 with mongoDB db v2.0.5, pdfile version 4.5. I have this setup in my 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.

$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! Example below:

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

Here is an example document in the collection 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.

Cursor Object is a 'key' here

The $cursor = $collection->find(); find() method will return a Cursor Object. Now you can use toArray() method to get your data. $dataArray = $cursor->toArray(); Simple as that. Or use foreach to get documents one by one . Ps. FindOne() which returns an array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM