我有一份Mongo 版的文件
{
"_id": {
"$oid": "649d3d688a1f30bf82e77342"
},
"test_value": {
"$numberLong": "10"
}
}
我想用这个Golang代码将"test_value"减go 一
jsonInput := []map[string]interface{}{
{
"$match": map[string]interface{}{
"test_value": 10,
},
},
{
"$set": map[string]interface{}{
"test_value": map[string]interface{}{
"$subtract": []interface{}{"test_value", 1}},
},
},
})
value, bsonByte, errMarshal := bson.MarshalValue(jsonInput)
if errMarshal != nil {
modules.DoLog("ERROR", "", "MongoService", "aggregateDocument", "cannot Marshal jsonInput to BSONByte", true, errMarshal)
ctx.IndentedJSON(200, gin.H{
"error": errMarshal.Error(),
})
return
}
fmt.Println(value)
bsonD := bson.A{}
errUnmarshal1 := bson.UnmarshalValue(value, bsonByte, &bsonD)
if errUnmarshal1 != nil {
modules.DoLog("ERROR", "", "MongoService", "aggregateDocument", "cannot Unmarshal BSONByte to BSOND", true, errUnmarshal1)
ctx.IndentedJSON(200, gin.H{
"error": errUnmarshal1.Error(),
})
return
}
_, err := Client.Database("rhanov_queries").Collection(collectionName).Aggregate(ContextMongo, bsonD)
if err != nil {
modules.DoLog("ERROR", "", "MongoService", "aggregateDocument", "cannot aggregate document to Mongo", true, err)
ctx.IndentedJSON(200, gin.H{
"error": err,
})
}
我得到了这个错误
"无法将文档聚合到Mongo.无法封送类型原语.A到BSON文档:Write数组只能在定位在元素或值上时写入数组,但定位在TopLevel上"
我做错什么了?