mgo:如何更新文档中的特定数组 [英] mgo: how to update a specific array in a document

查看:59
本文介绍了mgo:如何更新文档中的特定数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有这个文档:

<预><代码>[{_id":53dc97bf91f1f933e15d6fb1",属性":{冷藏":假的,易燃":假的,食物":假的,脆弱的":假的,冻结":假,热":假,超大":假},createdAt":02/08/14 09:48:16",客户":53d68bc091f1f933e15d6f90",位置":[{计数":0,仓库":53db430c91f1f933e15d6fa6"},{计数":34,仓库":53db430c91g1f933e45d6fa4"},],名称":测试",类型":存储",更新时间":"}]

如果我知道仓库 ID,如何更新位置数组?这就是我现在拥有的

coll := p.GetDb().C(产品")变化:= bson.M {位置":bson.M {$elemMatch":bson.M {仓库":bson.ObjectIdHex(warehouseId),},},$set":bson.M {location.$.count":4,},}错误 := coll.UpdateId(bson.ObjectIdHex(productId), changes)如果错误!= nil {http.Error(res, err.Error(), 500)返回}

但是得到这个错误:

<块引用>

'location.$elemMatch' 中以美元 ($) 为前缀的字段 '$elemMatch' 对存储无效.

解决方案

你写错了.仓库id"值的匹配属于语句的查询"部分,而不是更新"部分.因此,您不需要 UpdateId 变体,而是 Update 因为它允许更广泛的查询选择:

query := bson.M{"_id": bson.ObjectIdHex(productId),location.warehouse":bson.ObjectIdHex(warehouseId)}更新:= bson.M{$set":bson.M{location.$.count":4}}错误:= coll.Update(查询,更新)

另请注意点符号"form 在这里很好,因为数组元素的选择器只是一个单一的字段.您通常只需要 $elemMatch 当数组中有多个字段来建立匹配时.

I have this document inside my database:

[
  {
    "_id": "53dc97bf91f1f933e15d6fb1",
    "attributes": {
      "chilled": false,
      "flammable": false,
      "food": false,
      "fragile": false,
      "frozen": false,
      "hot": false,
      "outsized": false
    },
    "createdAt": "02/08/14 09:48:16",
    "customer": "53d68bc091f1f933e15d6f90",
    "location": [
      {
        "count": 0,
        "warehouse": "53db430c91f1f933e15d6fa6"
      },
      {
        "count": 34,
        "warehouse": "53db430c91g1f933e45d6fa4"
      },
    ],
    "name": "test",
    "type": "stored",
    "updatedAt": ""
  }
]

How can i update the location array if i know the warehouse Id? This is what i have now

coll := p.GetDb().C("product")
    changes := bson.M {
        "location": bson.M {
            "$elemMatch": bson.M {
                "warehouse": bson.ObjectIdHex(warehouseId),
            },
        },
        "$set": bson.M {
            "location.$.count": 4,
        }, 
    }
    
    err := coll.UpdateId(bson.ObjectIdHex(productId), changes)
    if err != nil {
        http.Error(res, err.Error(), 500)
        return
    }

But get this error:

The dollar ($) prefixed field '$elemMatch' in 'location.$elemMatch' is not valid for storage.

解决方案

You wrote this the wrong way around. The match on the warehouse "id" value belongs in the "query" portion of your statement and not in the "update" section. As such, you don't want the UpdateId variant, but the Update as it allows a wider query selection:

query := bson.M{
    "_id": bson.ObjectIdHex(productId),
    "location.warehouse": bson.ObjectIdHex(warehouseId)
}

update := bson.M{
    "$set": bson.M{
        "location.$.count": 4
    }
}

err := coll.Update(query,update)

Also note that the the "dot notation" form is fine here as your selector for the array element is just a singular field. You typically only need $elemMatch when there is more that one field in the array to establish the match.

这篇关于mgo:如何更新文档中的特定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆