2015-12-30 17 views
1

更新的值我有一个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. 

我还能尝试一下呢?

+1

你试过'MONGO [:DELIVERY_OPTIONS] .update_one({:_id => 1},{ “$设置”=> {“days.1.delivery_options。 11:00-12:00.orders“=> 1}})'? – chridam

+0

这工作。如果你复制粘贴这个答案我可以接受它。谢谢。 – narzero

回答

1

你也可以试试:

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

你可以试试下面的命令 -

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

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

) 

我假定集合名称是交易,但可以将其更改到您的收藏名字。