node.js - Mongojs attempting to push to array -


i'm trying keep list of sites i'm working on right now, , i'm having issues using $push.

i've created document this:

accountdata = {     'accountid': account_id,      sites: {         '001': 'example.com',     } };  db.accounts.insert(accountdata); 

this works great, get:

{ accountid: 'ac654164545', 'sites.001': { '$exists': true } } 

and add sites object. i'm trying:

db.accounts.update(       {'accountid': account_id},      {          $push:          {              sites:              {                  '002': 'example2.com'             }          }      },      function(err,doc){         console.log(err);     } ); 

the error is:

err: 'the field \'sites\' must array of type object in document 

i don't want document created array, know if did when inserting:

    sites: {         '002': 'example2.com',         '003': 'example3.com',         '004': 'example4.com',     } 

it work fine.

how use $push, or other command, add "sites" object without being array?

it can't array because i'm using following search existing sites.

search['sites.' + site_id] = { $exists : true }; 

ok, figured out problem was.

i wasn't thinking problem in right context.

the error correct in saying needed array, changed "sites" object array when created (initial insert), it's array:

    accountdata = {         'accountid': account_id,          sites: [{'001': 'example.com'}]     }; 

then, using exact same code above $push, worked properly.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -