getting result from mongodb with toArray()
I am using mongodb with php. I have a collection and inside this collection I store information like user_id and items. I want to get items from a specific user and I run this query below.
$record = $collection->find(
['user_id' => 1004],
['projection' => ['items' => 1, '_id' => 0]]
)->toArray();
But the result is more complicated because it contains a lot of information that I don't need. Do you have any idea how I can filter only the items that I have defined in the query (without any extra foreach loop to get only items)
Images are here
my collection in MongoDB Compass
and the response from records is
Out of curiosity, does your recently created GitHub account have any relation to AlfredJensen?
But the result is more complicated because it contains a lot of information that I don't need.
You are executing a query that matches on user_id and then ignores all but the items field. The cursor only returns an items field for each result. I'm not sure what "a lot of information that I don't need" is referring to here, as the screenshots you shared don't reveal anything about the structure embedded documents within the items array. I can only see that the items array has 1243 elements and the first element's document has 5 fields.
I'd encourage you to read through Project Fields to Return from Query and referenced documentation pages thoroughly. In particular:
If you're looking to merely reduce the number of items elements returned, the $slice or $elemMatch operators may be useful. If you were querying on array elements, which doesn't appear to be the case since you only filter by user_id, then the $ (positional) operator could be used.
If none of these projection operators are helpful, then you could look into using the aggregation framework, which provides a much richer set of operators to modify the document structure before it's returned to the client. In particular, the $unwind operator would allow you to break apart the items array into a stream of documents for individual processing. Since Collection::aggregate() also returns a cursor, working with the result would be similar to that of a basic query.
Thank you for your answer. No, I don't know this person.
What I want is for the response to look exactly like the first image or this code below.
{
"_id": {
"$oid": "62ecc9a35436000057005a96"
},
"user_id": 1004,
"items": [
{
"item_id": 2525,
"item_desc": "hastalavista",
"item_type": 41,
"item_number": "DKA0NXAZLXZ",
"item_cost": 69
}
another item
another item
.
I don't want each item to have an array, then object, then array, then item object. This structure is very complicated to handle, my structure is very simple items is array and has a lot of objects.
In the image 2 you can see the response to the query. Do you have any idea to make the response like this code above
Thanks a lot for your time!
@jmikola
What I want is for the response to look exactly like the first image or this code below.
If you do want _id and user_id included in the result object, then you'll need to change your query's projection. As-is, you're excluding all fields except items. To include only _id, user_id, and items, your projection should be: ['user_id' => 1, 'items' => 1] (_id is implicitly included).
I don't want each item to have an array, then object, then array, then item object. This structure is very complicated to handle, my structure is very simple items is array and has a lot of objects.
By default the PHP library returns documents and arrays as BSONDocument and BSONArray classes (see: [BSON] in the library documentation). Both of those classes extend PHP's ArrayObject, so you can access their fields as you would with a basic array or object in PHP. These classes are used to ensure that array and object types are preserved when writing data back to MongoDB and avoids edge cases with PHP arrays (see: notes in Emulating the Legacy Driver).
I think you're confused by the debug output. If you actually work with the result in PHP, you'll find that your root document is a single BSONDocument instance with directly accessible properties. Likewise for the items BSONArray and element BSONDocuments within it. So you should be able to do something like the following:
foreach ($document['items'] as $item) {
echo $item['item_desc'];
}
If that is still problematic for you, it's possible to change this behavior by specifying a typeMap option for the query. That's covered in the documentation I linked above and in much greater detail in the extension documentation under Deserialization from BSON.

