嘿,我正在使用go.mongodb.org/mongo-driver包与golang一起工作。这是我的代码:
package src
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
client *mongo.Client
stickerscol *mongo.Collection
)
func init() {
log.Println("Setting up database...")
var err error
client, err = mongo.Connect(context.Background(), options.Client().ApplyURI(Envars.DbUrl))
if err != nil {
log.Fatal(err)
}
log.Println("Connected to database")
stickerscol = client.Database("stickersbot").Collection("stickers")
chatcol = client.Database("stickersbot").Collection("chat")
}
func ChangeDefaultPack(userid int64, stickername string, format string) error {
fmt.Println(userid, stickername, format)
filter := bson.M{
"_id": userid,
"stickers.stickername": stickername,
"stickers.stickerpacktype": format,
}
updateOther := bson.M{
"$set": bson.M{
"stickers.$[elem].isdefault": false,
},
}
update := bson.M{
"$set": bson.M{
"stickers.$.isdefault": true,
},
}
arrayFilters := options.ArrayFilters{
Filters: []interface{}{bson.M{"elem.stickerpacktype": format}},
}
updateOptions := options.Update().SetArrayFilters(arrayFilters)
_, err := stickerscol.UpdateMany(context.TODO(), filter, updateOther, updateOptions)
if err != nil {
return err
}
ok, err := stickerscol.UpdateOne(context.Background(), filter, update, options.Update().SetUpsert(true))
if err != nil {
return err
}
if ok.ModifiedCount == 0 {
fmt.Println("no document matched")
return fmt.Errorf("no document matched")
}
return err
}
这就是db结构:
...
{
"_id": 1633375527,
"stickers": [
{
"stickerpacktype": "static",
"stickername": "gVtOy_1633375527_by_ChadManBot",
"isdefault": true
},
{
"stickerpacktype": "animated",
"stickername": "zIjsa_1633375527_by_ChadManBot",
"isdefault": true
},
{
"stickerpacktype": "video",
"stickername": "bJBle_1633375527_by_ChadManBot",
"isdefault": true
},
{
"stickerpacktype": "static",
"stickername": "iujiw_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "video",
"stickername": "CHnqb_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "XKJUP_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "animated",
"stickername": "pCFlC_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "animated",
"stickername": "jGGgt_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "animated",
"stickername": "cTxyA_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "animated",
"stickername": "ZfYXO_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "xJUkA_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "HIvsY_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "WIHFQ_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "OqjtE_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "QNysO_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "OrTsT_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "FzROg_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "QUBcT_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "NsZfM_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "AkxVM_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "Ddvus_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "LXfHV_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "sTaMv_1633375527_by_ChadManBot",
"isdefault": false
},
{
"stickerpacktype": "static",
"stickername": "vppgK_1633375527_by_ChadManBot",
"isdefault": false
}
],
"stickerpackcount": 2
}
...
我需要的代码是找到与给定ID和贴纸类型匹配的贴纸包列表,并将其默认值设置为false
,并将传递给func的包的默认值设置为true
。
基本上就是把一个设为默认值,其他的不设为默认值。有人能解释一下为什么这个函数没有返回文档吗?
1条答案
按热度按时间qmelpv7a1#
当有多个字段匹配时,应该使用
$elemMatch
运算符。否则,$
运算符不起作用。请参阅使用多个字段匹配更新嵌入文档。而且您不需要
UpdateMany
来更新单个文档中的多个数组元素。使用UpdateOne
代替。以下是基于您的代码和数据的演示:
运行演示,您将观察到文档的以下更改: