我需要从数组expenses
中删除{"category":"food","amount":{"$numberDouble":"12.0"}}
{"_id":{"$oid":"12"},
"chatID":{"$numberInt":"12"},
"expenses":[
{"category":"food","amount":{"$numberDouble":"12.0"}},
{"category":"food","amount":{"$numberDouble":"14.0"}}],
"income":[]}
我试过这个,它不起作用:
update := bson.M{"$pull": bson.M{"expenses": bson.D{{"category", "food"}, {"amount", 12}}}}
c.Coll.UpdateOne(context.TODO(), bson.M{"chatID": chatID}, update)
结果应该是这样的:
{"_id":{"$oid":"12"},
"chatID":{"$numberInt":"12"},
"expenses":[
{"category":"food","amount":{"$numberDouble":"14.0"}}],
"income":[]}
此更新操作运行良好:
update := bson.D{{ "$pull", bson.D{{ "expenses", bson.D{{ "category", "food" }, { "amount", 12.0 }}}}}}
filter := bson.D{{ "chatID", chatID }}
updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Matched %v documents and updated %v documents.n", updateResult.MatchedCount, updateResult.ModifiedCount)