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

时间:2018-07-18 04:51:16

标签: mongodb laravel laravel-5.5 jenssegers-mongodb

各位老兄,我是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或更多)?

下面是我在一个“收藏夹”下的示例注释数组格式。

"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)
                }
            ]
        }
    ]

谢谢。 :)

1 个答案:

答案 0 :(得分:0)

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

Post::raw()->updateMany(
    [],
    [ '$set' => ["comments.$[i].comments.$[j].description" => $desc] ],
    [ '$arrayFilters' => [
            [ 
                 [ "i.id" => "5b4dc367d282f" ],
                 [ "j.id" => "5b4dcfc7022db" ] 
            ]
        ]
    ]
)

希望它会有所帮助:)