-
Type:
Task
-
Resolution: Done
-
Priority:
Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: CRUD
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
I have the following CRUD operation:
filter := bson.M{"uuid": user.UUID, "cycles.dailyIntakes": bson.M{"$elemMatch": bson.M{"_id": ID}}} update := bson.M{"$push": bson.M{"cycles.$.dailyIntakes.$[i].macroNutrients": macroNutrientsWithIDAsString.MacroNutrients}} optionsArrayFilters := options.ArrayFilters{} dailyIntakeFilter := bson.M{"i._id": ID} optionsArrayFilters.Filters = append(optionsArrayFilters.Filters, dailyIntakeFilter) options := options.UpdateOptions{ArrayFilters: &optionsArrayFilters} _, err = collection.UpdateOne(nil, filter, update, &options)
I had to add array filters. I was under the impression that the filter I pass to the UpdateOne method would first retrieve the document I want based on my filter, and then update that document...
So if I am retrieving one document why is that I can't push like the following line:
update := bson.M{"$push": bson.M{"cycles.0.dailyIntakes.0.macroNutrients": macroNutrientsWithIDAsString.MacroNutrients}}
Notice the 0's.
I believe the 0's represent the index one wants to retrieve from an array. Is that correct?