如何在mongodb中更新json?

时间:2018-10-09 22:25:05

标签: mongodb

我有名为URL的mongodb集合。其中包含这样的数据。

db.urls.find({_ id:“ 80732090-cbc3-11e8-8e25-555812187031”})

{
  "_id": "80732090-cbc3-11e8-8e25-555812187031",
  "path": [
    "https://paper-stack.s3.us-west-1.amazonaws.com/paper/eenadu/magazine/Hyderabad/20181009/images/eenadu-20181009-0.png"
  ],
  "thumb": [
    "https://paper-stack.s3.us-west-1.amazonaws.com/paper/eenadu/magazine/Hyderabad/20181009/images/eenadu-20181009-0_thumb.png"
  ],
  "name": "Hyderabad",
  "type": "magazine",
  "Pdate": "20181009",
  "pname": "eenadu",
  "date": ISODate("2018-10-09T13:01:58.957Z"),
  "clips": [
    {
      "_id": "2e8f474c-aeb1-4221-9605-1e4b42c62807",
      "x": 572,
      "y": 217,
      "index": 0,
      "page": 0,
      "height": 201,
      "width": 1061,
      "imgWidth": 1920,
      "imgHeight": 2377
    },
    {
      "_id": "5905feb0-cbbe-11e8-b67c-a97ff541bba0",
      "x": 572,
      "y": 217,
      "index": 0,
      "page": 0,
      "height": 201,
      "width": 1061,
      "imgWidth": 1920,
      "imgHeight": 2377
    }
  ],
  "__v": 0
}

剪辑中有一个json,我想使用_id和clip._id更新剪辑中的高度。我该怎么办?

1 个答案:

答案 0 :(得分:0)

$positional operator)是您所需要的,就像这样

db.urls.updateOne(
    {
        '_id':'80732090-cbc3-11e8-8e25-555812187031', 
        'clips._id':'5905feb0-cbbe-11e8-b67c-a97ff541bba0'
    }, {
        $set: { 'clips.$.height': 123 }
    }
)