0

假设我有一个名为 ParrentObj 的集合和一个名为 ParrentObj 的相应 Eloquent 类:

[//ParentObj eloquent objects
{
    "_id":ObjectId("5e11ae242cb48f79afcd4b07"),
    "Code":"a"
    "Children":[
        //children eloquent objects
        {
            "Code":"a-a",
            "GrandChildren":[
                //GrandChildren eloquent objects
                {
                    "Code":"a-a-a"
                },
                {
                    "Code":"a-a-b"
                }
            ]           
        },
        {
            "Code":"a-b",
            "GrandChildren":[
            ]           
        }
    ],
},
{
    "_id":ObjectId("5e11b125e1f94bccca6784e9"),
    "Code":"b"
    "Children":[
    ]
}]

我很惊讶这行代码是如何正常工作的,因为所有的证据都证明了这一点:

$result = ParrentObj::find('5e11ae242cb48f79afcd4b07')->where('Children.Code', 'a-a')
                                              ->whereNotIn('Children.GrandChildren.Code',['a-a-a'])
                                              ->push('Children.$.GrandChildren', $arr);//results: no effect on db
$result = ParrentObj::find('5e11ae242cb48f79afcd4b07')->where('Children.Code', 'a-a')
                                              ->whereNotIn('Children.GrandChildren.Code',['a-a-c'])
                                              ->push('Children.$.GrandChildren', $arr);//results:add a GrandChildren subdocument under the a-a Children

“ParrenObj”是一个 Eloquent 对象,在 Children 字段上有一个 embedsMany 关系。

“Children”也是一个雄辩的对象,在 GrandChildren 字段上具有“embeds many”关系。(到 0 或 n 个“GrandChildren”雄辩的对象)

据我所知,查找的结果是一个雄辩的或集合,并结合结果将是一个查询生成器。当我试图得到这个结果时:

$result = ParrentObj::find($id)->where('Children.Code', "a-a")->first()

结果是 ParrentObj(雄辩的对象)。正如我所预料的,儿童领域有两个成员(不是一个)。如何雄辩地确定->push('Children.$.GrandChildren', $arr);应该应用在哪些孩子身上?

4

1 回答 1

0

它不适用于这种情况:

$result = ParrentObj::find('5e11ae242cb48f79afcd4b07')->where('Children.Code', 'a-b')
                                          ->whereNotIn('Children.GrandChildren.Code',['a-a-a'])
                                          ->push('Children.$.GrandChildren', $arr);

预期成绩:

在 ab Children 下添加一个带有代码 aaa 子文档的 GrandChildren

实际结果:

不会在 ab 下添加任何 GrandChildren,因为 whereNotIn 匹配了具有相同代码但在不同子项中的 GrandChildren

于 2020-01-05T13:20:40.390 回答