Discussion:
[nodejs] nodejs- how to use $set and $push on the same array in one update call in mongoose?
sonic nick
2018-07-12 20:50:15 UTC
Permalink
I have to initialize an array to [](empty array) and then push user chose
"tags" into it. This is my schema:

var user_schema = new Schema({
username:{type:String,required: true, unique : true, trim: true},
college:{type:String,required: true},
password:{type:String,required: true, trim: true},
email:{type:String,required: true, unique : true, trim: true},
phone:{type:Number,required: true, unique : true, trim: true},
dp:{type:String},
tags:[{type:String}],
description:{type:String},
skills:{type:String},
bucket:[{type:String}]
});

` and i have to initialize "tags" to [] and push user string values (user
entered/chosen) every time the request is made to the API call.

I am using this line of code:

stu_user.findOneAndUpdate(
{ _id: req.decoded.id },
{ $set:{tags:[]},$push: { tags: 'some chosen string' } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
});

But it's not working.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/75f1b2cb-4c30-4476-bafb-a1580c9ab0c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...