首页 > 解决方案 > 美元前缀字段对存储无效

问题描述

我收到了这个错误:

panic: multiple write errors: [{write errors: [{The dollar ($) prefixed field '$set' in 'conversations.newest_message.$set' is not valid for storage.}]}, {<nil>}]

这是我要更新的数据,我想在对话中更新 latest_message:

{
    "_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
    "conversations" : [ 
        {
            "conversation_id" : "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
            "newest_message" : {
                "user_name" : "",
                "data" : {
                    "description" : "",  
                },
            }
        }
    ]
}

这是 monoldb 查询:

db.collectionA.update(
    {
        _id: "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1", 
        "conversations.conversation_id": "VOoHg7nY4xBcrQtzxokbM9aStSSqei44"
    },
    {
        "$set": {
            "conversations.$.newest_message": {new_update_data_here}
    }
)

这是Golang代码,我使用的是mongodb驱动程序而不是mgo:

filter = bson.M{
                    "_id":                           id,
                    "conversations.conversation_id": messageReceived.ConversationID,
                }
update = bson.M{
                    "$set": bson.M{
                        "conversations": bson.M{
                            "newest_message": newestMessage,
                        },
                    },
                }

_, err = d.DB.Collection(collectionUserInformation).UpdateOne(ctx, filter, update)
            if err != nil {
                panic(err)
            }

标签: mongodbgo

解决方案


使用示例中的初始文档:

{
        "_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
        "settings" : {
                "general" : {
                        "languages" : [
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                }
                        ],
                        "contact" : "No one",
                        "language" : [
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                },
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                }
                        ]
                },
                "block_list" : [
                        "qGXVFjeZeFwrZjURfcZHqkCbgO91Gbu87KXx3Ba5",
                        "hFkF7DjPwBB6G2qbNT0J1ZROd3rxNLrZ5zt-HmMK"
                ],
                "auto_reply" : {
                        "set_auto_reply" : false,
                        "auto_reply_messages" : [ ]
                }
        },
        "conversations" : [
                {
                        "conversation_id" : "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
                        "people" : [
                                {
                                        "user_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
                                        "user_name" : "Dat Vo"
                                },
                                {
                                        "user_id" : "qGXVFjeZeFwrZjURfcZHqkCbgO91Gbu87KXx3Ba5",
                                        "user_name" : "An Son"
                                }
                        ],
                        "newest_message" : {
                                "user_name" : "",
                                "data" : {
                                        "description" : "",
                                        "host" : "",
                                        "image" : "",
                                        "title" : "",
                                        "type" : "",
                                        "url" : "",
                                        "youtube_video_id" : ""
                                },
                                "seen" : false
                        }
                }
        ]
}

您可以使用$ 位置运算符替换数组newest_message内的元素:conversations$set

更新 := bson.M{ "$set": bson.M{ "conversations.$.newest_message": bson.M{...

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

filter := bson.M{
    "_id":                           "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
    "conversations.conversation_id": "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
}
update := bson.M{
    "$set": bson.M{
        "conversations.$.newest_message": bson.M{
            "message_id": "saisa",
            "user_id":    "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
            "user_name":  "Dat Vo",
            "content":    "this is the new msg",
            "created_at": "12345678",
            "seen":       false,
        },
    },
}

_, err2 := d.DB.Collection(collectionUserInformation).UpdateOne(ctx, filter, update)
if err2 != nil {
    panic(err2)
}

它将返回以下更新的文档:

{
        "_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
        "settings" : {
                "general" : {
                        "languages" : [
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                }
                        ],
                        "contact" : "No one",
                        "language" : [
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                },
                                {
                                        "label" : "English",
                                        "value" : "eng"
                                }
                        ]
                },
                "block_list" : [
                        "qGXVFjeZeFwrZjURfcZHqkCbgO91Gbu87KXx3Ba5",
                        "hFkF7DjPwBB6G2qbNT0J1ZROd3rxNLrZ5zt-HmMK"
                ],
                "auto_reply" : {
                        "set_auto_reply" : false,
                        "auto_reply_messages" : [ ]
                }
        },
        "conversations" : [
                {
                        "conversation_id" : "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
                        "people" : [
                                {
                                        "user_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
                                        "user_name" : "Dat Vo"
                                },
                                {
                                        "user_id" : "qGXVFjeZeFwrZjURfcZHqkCbgO91Gbu87KXx3Ba5",
                                        "user_name" : "An Son"
                                }
                        ],
                        "newest_message" : {
                                "created_at" : "12345678",
                                "seen" : false,
                                "message_id" : "saisa",
                                "user_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
                                "user_name" : "Dat Vo",
                                "content" : "this is the new msg"
                        }
                }
        ]
}

您之前的尝试是用conversations一个conversations仅包含newest_message. 您希望将$set运算符与提供完整路径的位置$运算符一起使用,以便它仅替换您想要的子文档。


推荐阅读