Reputation: 1
I got this error:
panic: multiple write errors: [{write errors: [{The dollar ($) prefixed field '$set' in 'conversations.newest_message.$set' is not valid for storage.}]}, {<nil>}]
Here is the data I want to update, and I want to update newest_message inside conversations:
{
"_id" : "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
"conversations" : [
{
"conversation_id" : "VOoHg7nY4xBcrQtzxokbM9aStSSqei44",
"newest_message" : {
"user_name" : "",
"data" : {
"description" : "",
},
}
}
]
}
Here is the mongoldb query:
db.collectionA.update(
{
_id: "iNIXU8dgLgg-KdC3FE1hX8qBpY44hTF-2m-h-RG1",
"conversations.conversation_id": "VOoHg7nY4xBcrQtzxokbM9aStSSqei44"
},
{
"$set": {
"conversations.$.newest_message": {new_update_data_here}
}
)
and here is the Golang code, I'm using mongodb driver not 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)
}
Upvotes: 0
Views: 3073
Reputation: 425
Using as initial document the one from the 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
}
}
]
}
you can replace newest_message
element inside the conversations
array using $set
and the $ positional operator:
update := 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)
}
It will return the following updated document:
{
"_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"
}
}
]
}
Your previous attempt was replacing the conversations
array for a document with one doc conversations
that contained just newest_message
. You want to use the $set
operator with the positional $
operator providing the full path so it just replaces the sub-document you want.
Upvotes: 0