I have following document in a collection in a MongoDB database:
{
_id: 'anID',
data: { field1: 100, field2: 'str' }
}
Following code is able to find and update the document:
filter := bson.M{
"_id": "anID",
"data": bson.M{
"field1": 100,
"field2": "str",
},
}
newData := bson.M{
"field1": 200,
"field2": "str2",
}
update := bson.M{"$set": bson.M{"data": nextData}}
res, err := collection.UpdateOne(ctx, filter, update)
If I change the filter to following, the code is no longer able to find and update the document:
filter := bson.M{
"_id": "anID",
"data": bson.M{
"field2": "str",
"field1": 100,
},
}
How can I filter without depending on the order of fields in nested documents? Is using dot notation the only way to do this in a foolproof manner?