1

我有这种查询要运行。当键不存在时,手动运行此查询返回 OK,upsertedCount = 1

db.test.update({Key: 'random-id'}, {$inc: {Version: 1}},{upsert: true})

我尝试将其转换为下面的 mongodb golang 版本

client, _ := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017/"))
coll := client.Database("test").Collection("test")
filter := bson.D{bson.E{"Key", "random-id"}}
docs := bson.D{bson.E{"$inc", bson.E{"Version", 1}}}

upsert := true
result, err := coll.UpdateOne(
  context.TODO(),
  filter, docs,
  &options.UpdateOptions{Upsert: &upsert})
if err != nil {
  panic(err)
}
fmt.Print(result)

不幸的是,这个查询返回错误

multiple write errors: [{write errors: [{Cannot increment with non-numeric argument: {key: "Version"}}]}, {<nil>}]

为什么它不能工作?似乎驱动程序试图增加它而不将其发送到 mongo

编辑:

  1. 将模式大小写更改为 Upper,以遵循 go 代码
  2. 使用更简单的代码版本
4

1 回答 1

1

问题在于你的docs价值。它应该是一个有效的文件。bson.D如果其所有元素都有效,则它是有效文档。它有一个带键的元素$inc,它也要求它的值是一个有效的文档。bson.E不是文档,它是文档的一个元素。

将您更改docs为:

docs := bson.D{bson.E{"$inc", bson.D{bson.E{"Version", 1}}}}

它会起作用。

如果顺序不重要(在您的情况下不重要),或者您可以使用它bson.M来建模您的filterdocs这样的:

filter := bson.M{"Key": "random-id"}
docs := bson.M{
    "$inc": bson.M{"Version": 1},
}

这更简单、更清晰、更直观。

另请注意,选项有构建器。options.UpdateOptions像这样安全、惯用且清晰地获得您的价值:

options.Update().SetUpsert(true)
于 2020-05-06T16:34:49.657 回答