我收到此错误:
panic: multiple write errors: [{write errors: [{The dollar ($) prefixed field '$set' in 'conversations.newest_message.$set' is not valid for storage.}]}, {<nil>}]
这是我要更新的数据,我想在对话中更新newest_message:
{
"_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
"conversations" : [
{
"conversation_id" : "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
"newest_message" : {
"user_name" : "",
"data" : {
"description" : "",
},
}
}
]
}
这是mongoldb查询:
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)
}
最佳答案
使用example中的一个作为初始文档:
{
"_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
和$ positional operator替换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
运算符与提供完整路径的位置$
运算符一起使用,以便仅替换所需的子文档。关于mongodb - 美元前缀字段对存储无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60186855/