【问题标题】:some error in using $push in mongodb在 mongodb 中使用 $push 的一些错误
【发布时间】:2018-04-25 09:32:11
【问题描述】:

我的架构是

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);

我想用更新一些细节-

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"});
        }
    });
});

我没有在任何地方定义数组

但问题是更新时显示错误

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

我被困在这里

请有人帮我解决这个错误

【问题讨论】:

  • 如果我使用 $set 那么所有其他字段将被删除...
  • thnx ,它有效

标签: node.js mongodb mongoose schema


【解决方案1】:

您只需将$push 更改为$set。就是这样。

【讨论】:

  • 说得好,但以前我使用 $push 时效果很好。
  • 当你想将一个项目推入一个数组时,你可以使用 $push 。如果你想使用 $push 你可能会改变你的模型。
猜你喜欢
  • 1970-01-01
  • 2021-01-19
  • 2016-02-26
  • 2018-10-14
  • 1970-01-01
  • 2017-08-23
  • 2012-04-07
  • 2012-02-22
  • 2020-07-29
相关资源
最近更新 更多