user1954882
user1954882

Reputation: 330

Mongo Update $set not working

I'm trying to mass update some mongo documents.

I'm using the query

db.articles.update(
    { 
         'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"), 
         'source.importer': 'pa'
    }, 
    { 
         $set : 
             { 
                 'source.expires-at': ISODate("2014-01-01T08:39:45Z") 
             } 
    }
)

This query does not update the source.expires-at field, however the where part of the statement works fine.

The document structure is

{
   "_id": ObjectId("5211dc100000044707000015"),
   "categories": {
     "0": {
       "id": ObjectId("51cd5272222wb6zs464fa4d9")
    } 
  },
   "source": {
     "importer": "pa",
     "expires-at": ISODate("2013-09-18T08:49:32.0Z") 
  }
}

Upvotes: 11

Views: 19314

Answers (1)

Yalamber
Yalamber

Reputation: 7580

Try this:

db.articles.update(
  { 
    'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"), 
    'source.importer': 'pa'
  }, 
  { 
    $set: { 
      'source.expires-at': ISODate("2014-01-01T08:39:45Z") 
    } 
  }, 
  { multi: true }
)

You will need to pass additional option argument {multi: true};

Look in to this https://education.10gen.com/courses/10gen/M101JS/2013_August/courseware/CRUD/Multi-update/

Upvotes: 18

Related Questions