Golang MGO – 插入或更新不按预期工作

huangapple go评论113阅读模式
英文:

Golang MGO - Insert or update not working as expected

问题

我正在使用MGO包连接我的MongoDB数据库,运行在Go语言的网站上。

我正在处理用户的登录,并尝试使用Upsert()函数来更新用户,如果他们在数据库中存在,则插入。

问题是,当我运行Upsert()(下面的代码)时,它会替换所有字段,而不仅仅是更新第二个参数bson.M{}中存在的字段。

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"}, // 要更新的文档
  3. bson.M{"displayName": "Johhny"}, // 要替换的内容
  4. )

我尝试解释的一个可视化示例。

现有的数据库文档:

  1. {
  2. "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
  3. "email" : "someone@gmail.com",
  4. "password" : "",
  5. "age": 69,
  6. "displayName" : "Someone!"
  7. }

运行后:

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"},
  3. bson.M{"displayName": "My name was updated"},
  4. )

文档变为:

  1. {
  2. "_id" : ObjectId("58e789feab64da55ebcf691c"),
  3. "displayName" : "My name was updated"
  4. }

我期望文档变为:

  1. {
  2. "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
  3. "email" : "someone@gmail.com",
  4. "password" : "",
  5. "age": 69,
  6. "displayName" : "My name was updated" // 这个字段应该被更新,其他字段应该保持不变
  7. }

最后我的问题是。

如何在MongoDB集合中更新文档,如果文档已经存在,则插入它?

英文:

I'm running a website in Go and I'm using the MGO package to connect with my MongoDB database.

I am handling a user's sign in, and I am trying to use the func Upsert() to update a user if they exist in the database, otherwise insert them.

The issue is, when I run Upsert() (the code below), it replaces all fields rather than updating only the present fields in the second argument's bson.M{}.

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"}, // Which doucment to upsert
  3. bson.M{"displayName": "Johhny"}, // What to replace
  4. )

A visual example of what I'm trying to explain.

An existing database document:

  1. {
  2. "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
  3. "email" : "someone@gmail.com",
  4. "password" : "",
  5. "age": 69,
  6. "displayName" : "Someone!"
  7. }

After running:

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"},
  3. bson.M{"displayName": "My name was updated"},
  4. )

The document becomes:

  1. {
  2. "_id" : ObjectId("58e789feab64da55ebcf691c"),
  3. "displayName" : "My name was updated"
  4. }

When I expected the document to become:

  1. {
  2. "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
  3. "email" : "someone@gmail.com",
  4. "password" : "",
  5. "age": 69,
  6. "displayName" : "My name was updated" // This should be updated, all others should be left untouched
  7. }

Finally my question.

How can I update a document if it already exists in a MongoDB collection, otherwise insert it?

答案1

得分: 5

如果您想要使用提供的字段更新文档,并忽略所有其他字段,我认为在没有先进行选择的情况下是不可能的。

在stackoverflow上查看这个问题

编辑:
尝试以下代码:

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"},
  3. bson.M{"$set": bson.M{"displayName": "My name was updated"}},
  4. )
英文:

If you are trying to update a document with fields that you provide and ignore all other fields then I think it's not possible without a select first.

See this question on stack overflow

EDIT:
Try:

  1. db.C("users").Upsert(
  2. bson.M{"email": "someone@gmail.com"},
  3. bson.M{"$set": bson.M{"displayName": "My name was updated"}},
  4. )

huangapple
  • 本文由 发表于 2017年4月7日 20:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/43278696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定