MongoDB + Laravel + jenssegers / laravel-mongodb +更新嵌套的子元素

问题描述 投票:1回答:1

下层人士,我是MongoDB的新手,正在寻找答案

  1. 有什么方法可以在不循环的情况下更新嵌套数组。

    foreach ($post->comments as $key => $comment) {
    if ($comment['posted_by'] == $authUser['id']) {
        $data = $post->update([
            "comments.$key.description" => $dataArray['description'],
            "comments.$key.updated_at" => $dataArray['updated_at'],
        ]);
    }}
    

我想做下面的事情。

$post = Post::where('_id', $id)->where('comments.*.id', $commentId)->update(array('description' => $desc));

或者我必须为此编写原始的MongoDB查询。我在主注释下也有1级嵌套注释,因此,如果要更新嵌套注释,则必须循环注释数组而不是嵌套注释数组。

if ($subCommentId) {
    foreach ($comment as $nestedkey => $nestedComments) {
        if ($nestedComments['id'] === $subCommentId && $nestedComments['posted_by'] == $authUser['id']) {
            $data = $post->update([
                "comments.$key.$nestedkey.description" => $dataArray['description'],
                "comments.$key.$nestedkey.updated_at" => $dataArray['updated_at'],
            ]);
        }
    }
} 

类似这样的东西:

$post = Post::where('_id', $id)->where('comments.*.id', $commentId)->where('comments.*.*.id', $subCommentId)->update(array('description' => $desc));
  1. 将注释存储在与数组相同的集合中是否很好,还是应该为此创建一个新的集合,因为BSON文档的最大大小为16兆字节,并且可以存储多少注释(例如10K或更多)?

下面是我在一个Collection下的示例注释数组格式。

"comments" : [
        {
            "description" : "description some", 
            "channel" : "swachhata-citizen-android", 
            "user_role" : "Citizen", 
            "id" : "5b4dc367d282f", 
            "user_role_id" : ObjectId("5accd7f8309a203be03b6441"), 
            "created_at" : "2018-07-17 15:52:31", 
            "updated_at" : "2018-07-17 15:52:31", 
            "ip_address" : "127.0.0.1", 
            "user_agent" : "PostmanRuntime/6.4.1", 
            "deleted" : false, 
            "channel_id" : "5acccfe4309a2038347a5c47", 
            "posted_by" : NumberInt(1), 
            "comments" : [
                {
                    "description" : "some description nested", 
                    "channel" : "swachhata-citizen-android", 
                    "user_role" : "Citizen", 
                    "id" : "5b4dcfc7022db", 
                    "user_role_id" : ObjectId("5accd7f8309a203be03b6441"), 
                    "created_at" : "2018-07-17 16:45:19", 
                    "updated_at" : "2018-07-17 16:45:19", 
                    "ip_address" : "127.0.0.1", 
                    "user_agent" : "PostmanRuntime/6.4.1", 
                    "deleted" : false, 
                    "channel_id" : "5acccfe4309a2038347a5c47", 
                    "posted_by" : NumberInt(1)
                }
            ]
        }
    ]

感谢。 :)

mongodb laravel laravel-5.5 jenssegers-mongodb
1个回答
0
投票

要更新嵌套文档,应使用arrayFilters:

Post::where(
    [],
    [ '$set' => ["comments.$[i].comments.$[j].description" => $desc] ],
    [ '$arrayFilters' => [
            [ 
                 [ "i._id" => "5b4dc367d282f" ],
                 [ "j._id" => "5b4dcfc7022db" ] 
            ]
        ]
    ]
)

希望有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.