【问题标题】:Trying to update a document尝试更新文档
【发布时间】:2013-01-11 11:26:40
【问题描述】:

我想更新一个文档(将用户名“toto”放在模型 id 1 上)。问题是,update() 调用会发生任何事情(比如应用程序正在等待某事),而我不去更新回调。

我不明白发生了什么,这里是代码:

UserSchema : {
    username: {
        type: String,
        required: true,
        unique: true
    },
};

var UserSchema = new mongoose.Schema(UserSchema);
UserSchema
        .virtual('id')
        .get(function(){
            return this.get('_id');
        }).set(function(id){
            return this.set('_id', id);
        });    

var User = db.model('User', UserSchema);
var Entity = new User();

Entity.update({ _id: 1 }, { username: 'toto'}, null, function(error, numAffected){
    if (error){
        console.log("|-->Error Query trying to update model");
    }else{
        console.log("|-->Update model succeed");
    }
});

谢谢!

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    我的问题是在其实例上实例化 Use 和 update(),而不是将 update() 调用应用于 UserSchema。

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2023-04-10
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多