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.