Rupesh yadav
Rupesh yadav

Reputation: 7

some error in using $push in mongodb

my Schema is

var User = new mongoose.Schema({
    //general information
    name : {type:String},
    email : {type : String},
    number : {type : String},
    password : {type:String},
    //featured information
    path: { type: String},
    dob : {type:String},
    gender : {type : String},
    blood_group : {type:String},
    marital_status : {type : String}
});

module.exports = mongoose.model('user',User);

i want to use update some details by-

app.post('/contactinfo',function (req,res) {
    var name = req.body.name;
    var number = req.body.number;
    var email = req.body.email;
    User.update({_id : userID},{
        $push : {
            name : name,
            number : number,
            email : email
        }
    },{new : true },function (err,result) {
        if(err){
            console.log(err);
        }
        else{
            console.log(result);
            res.send({status : "success" , message : "Details updates"});
        }
    });
});

I don't define array anywhere

but the problem is that is show error on updating

{ MongoError: The field 'name' must be an array but is of type string in document {_id: ObjectId('5a0880e2d767e8131807e7f4')}

I am stuck here

please someone help me in solving this error

Upvotes: 0

Views: 1724

Answers (1)

Pavan Vora
Pavan Vora

Reputation: 1734

You just have to change your $push to $set. That's it.

Upvotes: 5

Related Questions