I have stored a document set in MongoDB like below where ID is generated automatically through PHP
{
"_id": {
"$oid": "5ff745237d1b0000860007a6"
},
"user": "5ff741fb7d1b0000860007a4",
"name": "Test",
"slug": "fed73b70d080584c21e0a4353825ef91",
"category": "5fef4a467d1b000086000745",
"images": [{
"100x128": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"200x256": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"300x385": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"500x642": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg"
},
{
"100x128": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"200x256": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"300x385": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"500x642": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg"
}],
}
Now the problem is I want to get value from images object
from key 100x128
only from the first array.
I am using the below code to achieve
$productsArray = $collection->aggregate(
[
[
'$match' => [
'user_id' => $user_id
]
],
[ '$unwind' => '$images' ],
[ '$unwind' => '$images.100x128' ],
[
'$project' => [
'name' => 1,
'image' => '$images'
]
]
]
);
and I always get an output as below
MongoDBModelBSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[_id] => MongoDBBSONObjectId Object
(
[oid] => 5ff745357d1b0000860007a7
)
[name] => Test 1
[image] => MongoDBModelBSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[100x128] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[200x256] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[300x385] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[500x642] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
)
)
)
)
I am not sure how to get just that specific value. Where am i going wrong here?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…