dqhgjay5753 2013-09-19 13:47
浏览 77
已采纳

使用PHP获取ID中的GridFS文件

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?

  • 写回答

1条回答 默认 最新

  • doulun7739 2013-10-21 17:51
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料