问题描述:

I am using MongoDB GridFS to store some user avatars. My backend is PHP.

On some page I need to display all images with single user. So I have the Mongo IDs. How do I perform a find query where _id is in given IDs. In mongo the query would be {"_id: {$in: [id1,id2]}.

In PHP I have:

$gridFs = $this->getGridFs();

$photos = $gridFs->find();

where find accepts 2 arrays. If I was finding single file it would be:

$photos = $gridFs->find(array('id' => new MongoId($id)));

So what about several results?

And also I wander if it is possible to find with limit and offset?

网友答案:

MongoGridFS::find() returns a MongoGridFSCursor, which extends MongoCursor. ->skip() and ->limit() are available on the MongoCursor.

The MongoGridFS::find() syntax is the same as the MongoCollection::find() syntax.

$MongoGridFSCursor = $MongoGridFSObject->find(array(
    'id' => array('$in' => array($myMongoID1, $myMongoID2))
));

You can then skip over some results and limit them:

$MongoGridFSCursor->skip(3)->limit(8);

and finally iterate over the results:

foreach($MongoGridFSCursor as $MongoGridFSFile) {
}

I suspect your example is incorrect, and should be _id, not id, in your find query?

相关阅读:
Top