如何更新多层文档中的值

时间:2015-12-30 15:04:08

标签: ruby mongodb

我在MongoDB集合中有以下文档:

{
   "_id":1,
   "days":[
      {
         "date":"30-12-2015",
         "label":"woensdag 30/12",
         "delivery_options":{
            "10:00-11:00":{
               "capacity":5,
               "orders":0,
               "remaining_capacity":5
            },
            "11:00-12:00":{
               "capacity":5,
               "orders":0,
               "remaining_capacity":5
            },
            ...
         }
      },
      {
         "date":"31-12-2015",
         "label":"donderdag 31/12",
         "delivery_options":{
            "10:00-11:00":{
               "capacity":5,
               "orders":0,
               "remaining_capacity":5
            },
            "11:00-12:00":{
               "capacity":5,
               "orders":0,
               "remaining_capacity":5
            },
            ...
         }
      },
      ...
   ]
}

我想要做的是将document['days'][1]['delivery_options']['11:00-12:00]['orders']的值更新为1

以下是我正在尝试的内容:

MONGO[:delivery_options].find(_id: 1).update_one($set => {"days.1.delivery_options.11:00-12:00.orders" => 1})

但是回归:

BSON::InvalidKey: NilClass instances are not allowed as keys in a BSON document.

我还能尝试什么?

2 个答案:

答案 0 :(得分:1)

您也可以尝试:

MONGO[:delivery_options].update_one({ :_id => 1 }, { "$set" => { "days.1.delivery_options.11:00-12:00.orders" => 1 }})

答案 1 :(得分:0)

您可以尝试以下命令 -

db.deals.update(
     {"_id":ObjectId("5683fc08cc7d3c425b573125")},

        {$set:{ "days.1.delivery_options.11:00-12:00.orders":1 }}
        , { upsert: true }

  )

我假设收藏品名称是商品,但您可以将其更改为收藏品名称。