【问题标题】:Get GridFS files within ids with PHP使用 PHP 在 ids 中获取 GridFS 文件
【发布时间】:2013-09-19 13:47:20
【问题描述】:

我正在使用 MongoDB GridFS 来存储一些用户头像。我的后端是 PHP。

在某些页面上,我需要以单个用户显示所有图像。所以我有 Mongo ID。如何在给定 ID 中的 _id 处执行查找查询。在 mongo 中,查询将是 {"_id: {$in: [id1,id2]}。 在 PHP 中我有:

$gridFs = $this->getGridFs();
$photos = $gridFs->find();

其中 find 接受 2 个数组。如果我找到单个文件,它将是:

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

那么几个结果呢? 如果可以找到限制和偏移量,我也会徘徊?

【问题讨论】:

    标签: php mongodb gridfs


    【解决方案1】:

    MongoGridFS::find() 返回一个MongoGridFSCursor,它扩展了MongoCursor。 ->skip() 和 ->limit() 在 MongoCursor 上可用。

    MongoGridFS::find() 语法与MongoCollection::find() syntax 相同。

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

    然后您可以跳过一些结果并限制它们:

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

    最后对结果进行迭代:

    foreach($MongoGridFSCursor as $MongoGridFSFile) {
    }
    

    我怀疑您的示例不正确,在您的查找查询中应该是 _id,而不是 id?

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2013-06-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多