narzero
narzero

Reputation: 2299

How to update a value in a multi-layered document

I have the following document inside a MongoDB collection:

{
   "_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
            },
            ...
         }
      },
      ...
   ]
}

What I'd like to do is update the value of document['days'][1]['delivery_options']['11:00-12:00]['orders'] to 1.

Here's what I'm currently trying:

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

But that returns:

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

What else could I try?

Upvotes: 1

Views: 912

Answers (2)

chridam
chridam

Reputation: 103365

You can try also:

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

Upvotes: 1

Yaron Idan
Yaron Idan

Reputation: 6765

You can try the following command -

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

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

  )

I've assumed the collection name is deals, but you can change it to your collection name.

Upvotes: 0

Related Questions