node.js - How to call $addToSet if an array exists if it doesnt then call $set in one query? -


hey trying set new array not exist in mongodb docs, want able throw additional documents array without replacing values.

for example:

users {     id: 'value1',     name: {value2},     friends: [                {friendid: 'value x', type: 'valuey', tags: [tag1, tag2]}               ] 

basically tags not exist , want create , add tag1, , if exist want add tag2 array...

this make sense of variables in sample above have in db:

friendstatus = friends, fuid = friendid, labels = tags 

here attempt:

friend.findoneandupdate({userid: mongoose.types.objectid(req.signedcookies.userid),  'friendstatus.fuid': mongoose.types.objectid(req.body.user)},  {$addtoset: {'friendstatus.$.labels': req.body.labelnew}}     , function(err, userx) {       } 

currently there no way want mongodb update syntax.

here feature allow that: https://jira.mongodb.org/browse/server-6566


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -