如何在Mongodb / Golang中删除数组中的元素?

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

How do I remove an array item in Mongodb / Golang?

问题

我有以下数据结构,并且我正在尝试从'artists'数组中删除一个项目。

[
    {
        "id": "56b26eeb4a876400011369e9",
        "name": "Ewan Valentine",
        "email": "ewan@test.com",
        "artists": [
            "56b26f334a876400011369ea",
            "56b2702881318d0001dd1441",
            "56b2746fdf1d7e0001faaa92",
        ],
        "user_location": "Manchester, UK"
    }
]

这是我的函数...

// 从用户中删除艺术家
func (repo *UserRepo) RemoveArtist(userId string, artistId string) error {
    change := bson.M{"artists": bson.M{"$pull": bson.ObjectIdHex(artistId)}}
    fmt.Println(userId)
    err := repo.collection.UpdateId(bson.ObjectIdHex(userId), change)
    return err
}

我得到了以下错误:

{
  "_message": {
    "Err": "The dollar ($) prefixed field '$pull' in 'artists.$pull' is not valid for storage.",
    "Code": 52,
    "N": 0,
    "Waited": 0,
    "FSyncFiles": 0,
    "WTimeout": false,
    "UpdatedExisting": false,
    "UpsertedId": null
  }
}
英文:

I have the following data structure, and I'm attempting to remove an item from the 'artists' array.

[
    {
        "id": "56b26eeb4a876400011369e9",
        "name": "Ewan Valentine",
        "email": "ewan@test.com",
        "artists": [
            "56b26f334a876400011369ea",
            "56b2702881318d0001dd1441",
            "56b2746fdf1d7e0001faaa92",
        ],
        "user_location": "Manchester, UK"
    }
]

Here's my function...

// Remove artist from user
func (repo *UserRepo) RemoveArtist(userId string, artistId string) error {
	change := bson.M{"artists": bson.M{"$pull": bson.ObjectIdHex(artistId)}}
	fmt.Println(userId)
	err := repo.collection.UpdateId(bson.ObjectIdHex(userId), change)
	return err
}

I'm getting the following error:

{
  "_message": {
    "Err": "The dollar ($) prefixed field '$pull' in 'artists.$pull' is not valid for storage.",
    "Code": 52,
    "N": 0,
    "Waited": 0,
    "FSyncFiles": 0,
    "WTimeout": false,
    "UpdatedExisting": false,
    "UpsertedId": null
  }
}

答案1

得分: 5

$pull 操作符是更新语句中的“顶级”操作符,所以你的顺序是错误的:

change := bson.M{"$pull": bson.M{"artists": bson.ObjectIdHex(artistId)}}

更新操作符的顺序总是先操作符,再执行动作。

如果在“顶级”键中没有操作符,MongoDB会将其解释为一个“普通对象”来更新并“替换”匹配的文档。因此会出现关于键名中 $ 的错误。

英文:

The $pull operator is a "top level" operator in update statements, so you simply have this the wrong way around:

    change := bson.M{"$pull": bson.M{"artists": bson.ObjectIdHex(artistId)}}

The order of update operators is always operator first, action second.

If there is no operator at the "top level" keys, MongoDB interprets this as just a "plain object" to update and "replace" the matched document. Hence the error about the $ in the key name.

huangapple
  • 本文由 发表于 2016年2月7日 06:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/35247241.html
匿名

发表评论

匿名网友

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

确定