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

时间:2019-07-14 00:15:02

标签: php mongodb

我在一个集合中有多个文档(称为“档案”),并且想使用PHP检索与特定过滤器匹配的所有文档。这是我在这些收藏集中的文档的一个示例:

{ 
    "_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" : "" 
}

在我的PHP脚本中,我正在执行一个查询,以检索特定作者的文档,如下所示:

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

在一个示例中,我想获取作者“ stingybaku”的所有文档,并且如上述文档所示,有多个符合此条件的文档,但是,当我打印结果时,我总是得到只有一个文档,第一个:

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

这就是我所看到的:

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] => 
)

这是我第一次使用mongodb做某事,所以我想知道是否缺少某些东西来获得我想要的所有结果?

谢谢! 朱利奥。

0 个答案:

没有答案