I was struggling with this query for about three hours without results. I have this Mongoose Schema:
var User = mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
name: { type: String, required: true },
surname: { type: String, required: true },
tags: [{
type: { type: String },
name: { type: String }
}, { _id: false }]
};
and i need to perform some manipulation on the tags array. What i need to do is to simply add and remove tags sended by the client. I have created two nested query to do that:
updateTagsById: function(id, obj){
var add = [];
var remove = [];
for (var i = 0; i < obj.add.length; i++) {
add.push({ type: obj.type, name: obj.add[i] });
}
for (var i = 0; i < obj.remove.length; i++) {
remove.push({ type: obj.type, name: obj.remove[i] });
}
User.update({_id: id} , { $pushAll:{ tags: add } }, function(err){
if(err) throw err;
User.update({_id: id} , { $pullAll: { tags: remove } }, function(err){
if(err) throw err;
console.log('ok');
return true;
});
});
}
All the function is executed without error but if i go to the mongo shell i can see that only the push query goes well. I've tried various type of query without results. Can anyone help me understand why the second query it doesn't work?
via Robert
No comments:
Post a Comment