如何使用$cond在Golang中运行MongoDB查询
我尝试在Golang中运行MongoDB查询,但发现了一些错误。 如果字段“section.gateBack”等于0,我想用新的时间戳更新字段“expiresAt” 数据模型如下所示:如何使用$cond在Golang中运行MongoDB查询,mongodb,go,Mongodb,Go,我尝试在Golang中运行MongoDB查询,但发现了一些错误。 如果字段“section.gateBack”等于0,我想用新的时间戳更新字段“expiresAt” 数据模型如下所示: { "_id": "78b0e7de-c24c-11eb-8529-0242ac130003", "expiresAt": "2021-04-22T14:06:55.069Z", "section":
{
"_id": "78b0e7de-c24c-11eb-8529-0242ac130003",
"expiresAt": "2021-04-22T14:06:55.069Z",
"section": {
"gateFront": 1,
"gateMiddle": 1,
"gateBack": 0
}
}
cannot transform type []primitive.M to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel
update := bson.M{
"$set": bson.M{
"expiresAt": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateBack": bson.M{"$eq": 0}},
{"section.gateBack": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 0, 1),
"else": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateMiddle": bson.M{"$eq": 0}},
{"section.gateMiddle": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 1, 0),
"else": ...,
},
},
},
},
},
}
在第一次尝试中,我编写了查询,就像编写其他(简单的)$set操作一样,但这次使用$cond来处理if-else:
但通过此更新操作,我得到以下错误:
multiple write errors: [{write errors: [{The dollar ($) prefixed field '$cond' in 'expiresAt.$cond' is not valid for storage.}]}, {<nil>}]
但现在我得到了如下错误:
{
"_id": "78b0e7de-c24c-11eb-8529-0242ac130003",
"expiresAt": "2021-04-22T14:06:55.069Z",
"section": {
"gateFront": 1,
"gateMiddle": 1,
"gateBack": 0
}
}
cannot transform type []primitive.M to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel
update := bson.M{
"$set": bson.M{
"expiresAt": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateBack": bson.M{"$eq": 0}},
{"section.gateBack": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 0, 1),
"else": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateMiddle": bson.M{"$eq": 0}},
{"section.gateMiddle": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 1, 0),
"else": ...,
},
},
},
},
},
}
我不知道如何构建操作以使其可运行
我的目标是根据'section'的值,使用不同的时间戳更新字段expiresAt。我还想使嵌套if-else更复杂,但应该首先处理简单的条件
大概是这样的:
{
"_id": "78b0e7de-c24c-11eb-8529-0242ac130003",
"expiresAt": "2021-04-22T14:06:55.069Z",
"section": {
"gateFront": 1,
"gateMiddle": 1,
"gateBack": 0
}
}
cannot transform type []primitive.M to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel
update := bson.M{
"$set": bson.M{
"expiresAt": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateBack": bson.M{"$eq": 0}},
{"section.gateBack": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 0, 1),
"else": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": []bson.M{
{"section.gateMiddle": bson.M{"$eq": 0}},
{"section.gateMiddle": bson.M{"$exists": true}},
},
},
"then": time.Now().AddDate(0, 1, 0),
"else": ...,
},
},
},
},
},
}
有人知道如何在Golang中编写一个updateOne操作,它将if else作为$cond使用吗?使用
bson.A{bson.M{…},bson.M{…}
而不是[]bson.M{…}
mongo驱动程序别名映射和切片类型,并查找bson.M{}
bson.A{}
转换查询时键入。为什么不将“section.gateBack”:0
条件作为筛选器的一部分?@prasad_uu我不想将该条件添加到筛选器中,因为如果“section.gateBack”:0不匹配,我想在else部分签入下一个值“section.gateBack”:0。如果不匹配,则检查“section.gateFront”:0。因此,如果三个值中的一个为零,则您要更新?$cond
是聚合运算符,而不是更新运算符。为了将聚合运算符与4.2+一起使用,您需要传递一组管道阶段作为更新,mongodb应该使用驱动程序中的基元类型,这样我们就可以像定义“go but”一样定义查询了\_(ツ)_/¯