简体   繁体   English

如何在 MongoDB PHP 的集合中获取嵌套在文档内的 object 值

[英]How to get a object value nested inside document in a collection in MongoDB PHP

I have stored a document set in MongoDB like below where ID is generated automatically through PHP我已经在 MongoDB 中存储了一个文档集,如下所示,其中 ID 是通过 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.现在的问题是我想从第一个数组的键100x128images object中获取值。

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我总是得到一个 output 如下

MongoDB\Model\BSONDocument Object
(
   [storage:ArrayObject:private] => Array
    (
        [_id] => MongoDB\BSON\ObjectId Object
            (
                [oid] => 5ff745357d1b0000860007a7
            )

        [name] => Test 1
        [image] => MongoDB\Model\BSONDocument 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?我在哪里错了?

As far as i am getting your question it looks like you would need to understand the mongo concept.就我收到您的问题而言,您似乎需要了解 mongo 概念。 So you might get an array representation in UI or in PHP but at the end of day it is object.因此,您可能会在 UI 或 PHP 中获得数组表示,但最终它是 object。

Look closely the format that you have posted there is no array but multiple {} in that object just like JS so forget about array concept here.仔细查看您发布的格式,object 中没有数组,但有多个{} ,就像 JS 一样,所以在这里忘记数组概念。

If my understanding is correct this is what you are looking for如果我的理解是正确的,这就是你要找的

$productsArray = $collection->aggregate(
[
    [
        '$match' => [
                       'user_id' => $user_id
                    ]
    ],
    [ '$unwind' => '$images' ],
    [ '$unwind' => '$images.100x128' ],
    [
        '$project' =>   [
                           'name' => 1,
                           'image' => '$images.100x128'
                        ]
    ]
]

); );

Whatever you have googled or used it is correct just you need to add the second unwind value ie $images.100x128 instead of $images in the $project variable.无论您使用谷歌搜索或使用它都是正确的,您只需在$project变量中添加第二个展开值,即$images.100x128而不是$images

You can understand this way that $unwind variable uses recursive logic to get output as you want.您可以这样理解$unwind变量使用递归逻辑来根据需要获取 output 。 Like let's say you have an array inside an array first it will get values from internal array then it will come to parent array and then so on.就像假设您在数组中首先有一个数组,它将从内部数组中获取值,然后它将到达父数组,然后依此类推。 So the last value in $unwind variable should be used.所以应该使用$unwind变量中的最后一个值。

I am not sure if it will work on multiple objects but as you said you just need the first one.我不确定它是否适用于多个对象,但正如您所说,您只需要第一个。 This should do the charm.这应该有魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM