In my mongoDB document, I am looking to insert additional data:
{
"_id" : "7KufvMQFyyeuKFP68",
"target" : {
"10" : "true",
"id" : "ePce6fBAHx9KeKjuM"
}
}
I want to update the existing fields in target
or add new ones if needed. Here is the code I attempted:
var result = { "30": "true", "id" : "ePce6fBAHx9KeKjuM" };
Collection.upsert(
{ _id: id },
{ $set: { target: result } }
);
However, when I ran this code, the value of 10
was replaced by 30
, while what I actually wanted was:
{
"_id" : "7KufvMQFyyeuKFP68",
"target" : [{
"10" : "true",
"30" : "true",
"id" : "ePce6fBAHx9KeKjuM"
}]
}