Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo Update $set not working

Tags:

mongodb

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") 
  }
}
like image 949
user1954882 Avatar asked Aug 29 '13 08:08

user1954882


1 Answers

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/

like image 80
Yalamber Avatar answered Oct 12 '22 18:10

Yalamber