drfqfuhej48511519 2019-07-14 00:15
浏览 44

如何在PHP中从mongodb检索多个文档?

I have multiple documents in a collection (called "archives"), and using PHP I want to retrieve all the documents that match a specific filter. Here is a sample of the documents I have in that collections:

{ 
    "_id" : ObjectId("5d21382716b41f541350d472"), 
    "gameSystem" : "40k", 
    "archiveName" : "Test Archive 02", 
    "archiveDescription" : "Lorem Ipsum shit.", 
    "dateCreated" : "2019-07-06", 
    "lastUpdated" : "2019-07-06", 
    "gloryPoints" : 0, 
    "bannerImage" : "Aggressors.jpg" 
}

{ 
    "_id" : ObjectId("5d213bea16b41f59ac7e8b42"), 
    "author" : "stingybaku", 
    "gameSystem" : "40k", 
    "archiveName" : "Test Archive 03", 
    "archiveDescription" : "dsadfasgtsrgdfsg", 
    "dateCreated" : "2019-07-06", 
    "lastUpdated" : "2019-07-06", 
    "gloryPoints" : 0, 
    "bannerImage" : "" 
}

{ 
    "_id" : ObjectId("5d23f78316b41f44b1755e42"), 
    "author" : "stingybaku", 
    "gameSystem" : "40k", 
    "archiveName" : "test 4", 
    "archiveDescription" : "lkjhfasdjhladjhflahsdf", 
    "dateCreated" : "2019-07-08", 
    "lastUpdated" : "2019-07-08", 
    "gloryPoints" : 0, 
    "bannerImage" : "" 
}

And in my PHP script, I'm doing a query that retrieves the documents by a specific author, like this:

$filter = ['author' => $username];
$myarchives = new MongoDB\Driver\Query($filter);
$res = $mng->executeQuery("ttg.archives", $myarchives);
$myarchives = current($res->toArray());

In one example, I want to get all the documents by author "stingybaku", and as seen in the documents above, there are more than one documents that match this criteria, however, when I print the results, I always get only one document back, the first one:

echo '<pre>';
print_r($myarchives);
echo '</pre>';

And this is all I see:

stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5d213bea16b41f59ac7e8b42
        )

    [author] => stingybaku
    [gameSystem] => 40k
    [archiveName] => Test Archive 03
    [archiveDescription] => dsadfasgtsrgdfsg
    [dateCreated] => 2019-07-06
    [lastUpdated] => 2019-07-06
    [gloryPoints] => 0
    [bannerImage] => 
)

This is the first time I do something with mongodb, so I'm wondering if I'm missing something to get all the results that I want?

Thanks! Julio.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP