mongodb - 美元前缀字段对存储无效
问题描述
我收到了这个错误:
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)
}
解决方案
使用示例中的初始文档:
{
"_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
运算符与提供完整路径的位置$
运算符一起使用,以便它仅替换您想要的子文档。
推荐阅读
- java - 我的应用程序不会自动从 API 23 读取短信
- python-3.x - 如何用python编写一个简单的区块链
- reactjs - Change hover style under custom component in styled-component
- laravel - “如何修复 Laravel 中的‘找不到类’错误”
- php - ubuntu 中的包安装过程
- python - Django - 如果object_list ==空,图像不会显示
- assembly - 组装 TASM 大型模型程序(从 C 调用)矩阵循环
- javascript - 自动删除特定频道中的消息
- javascript - 无法读取未定义的属性“prop”
- angular - 角弹簧引导到一个项目和一个端口