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

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

Golang MGO - Insert or update not working as expected

问题

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

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

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

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

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

现有的数据库文档:

{
    "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
    "email" : "someone@gmail.com",
    "password" : "",
    "age": 69,
    "displayName" : "Someone!"
}

运行后:

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

文档变为:

{
    "_id" : ObjectId("58e789feab64da55ebcf691c"),
    "displayName" : "My name was updated"
}

我期望文档变为:

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

最后我的问题是。

如何在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{}.

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

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

An existing database document:

{
    "_id" : ObjectId("58e7589bab64da55ebcf5d25"),
    "email" : "someone@gmail.com",
    "password" : "",
    "age": 69,
    "displayName" : "Someone!"
}

After running:

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

The document becomes:

{
    "_id" : ObjectId("58e789feab64da55ebcf691c"),
    "displayName" : "My name was updated"
}

When I expected the document to become:

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

Finally my question.

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

答案1

得分: 5

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

在stackoverflow上查看这个问题

编辑:
尝试以下代码:

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

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:

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

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:

确定