在使用Upsert更新为true时未设置默认值 [英] Default value not set while using Update with Upsert as true

查看:530
本文介绍了在使用Upsert更新为true时未设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为用户提供以下模型:

I have the following model for users:

var UserSchema = new mongoose.Schema({

    name: String,
    dob: Date,
    sex: String,
    photo: String,
    email: {type: String, index: {unique: true, required: true}},
    created: {type: Date, default: Date.now}
});

var User = mongoose.model('Users', UserSchema);

如您所见,创建"字段采用当前日期的默认值,以便在创建新用户时自动设置该日期.

As you can see the 'created' field takes a default value of the current date so that it is automatically set when a new user is created.

发布用户详细信息时,我使用以下查询:

I use the following query when user details are posted:

User.findOneAndUpdate({email: user.email}, user, {upsert: true}, function (err, user) {
            if (err) {
                return callback (err);
            } else {
                callback(null, user);
            }
        });

findOneAndUpdateupsert: true结合使用的目的是返回一个现有的配置文件,或创建一个新的配置文件.它还会根据发布的数据更新所有字段.

The purpose of using findOneAndUpdate with upsert: true is to either return an existing profile, or create a new one. It also updates any fields based on the data posted.

但是,即使未发布创建的字段,created字段也会每次都使用当前日期进行更新.如何确保仅将该字段设置一次?

However, the created field gets updated with the current date each time, even though the created field is not posted. How can I make sure that this field is set only once?

编辑

数据库中的示例对象:

{
    "_id" : ObjectId("54620b38b431d48bce7cab81"),
    "email" : "someone@google.com",
    "__v" : 0,
    "name" : "somone",
    "sex" : "male"
}

事实证明,即使使用upsert创建新对象时,也未设置created字段.猫鼬只根据架构返回当前日期,即使该日期不存在于文档中.

It turns out that the created field is not being set even while creating a new object using upsert. Mongoose just returns the current date based on the schema even though it does not exist in the document.

因此,现在的问题是:如何确保使用upsert为参数中未提供的字段创建默认值?

So, the question now becomes: How do I make sure that using upsert creates the default value for a field not supplied in the arguments?

推荐答案

findOneAndUpdate仅发送MongoDB findAndModify请求(请参见

findOneAndUpdate simply sends a MongoDB findAndModify request (see findOneAndUpdate). What this means is that it skips all the mongoose magic involved with the schema setters, getters, defaults, etc. Validation is only run on create/save so the way around this is to do a .findOne(), check existence/create a new one, and then .save().

参见此问题以获取更多讨论

关于关于每次更改日期的第一个问题,您可以稍微更改架构.摆脱默认值,而是在声明模式之后添加此值:

In regards to the first question about changing the date each time, you could change the schema a bit. Get rid of the default value, and instead add this after declaring the schema:

UserSchema.pre("save", function (next) {
    if (!this.created) {
        this.created = new Date();
    }
    next();
});

这只会在不存在created:值的情况下创建日期,并应防止它每次(在使用.save()时)更改创建日期.

That will only create a date if the created: value is not present, and should prevent it from changing the creation date each time (when using .save()).

请参阅猫鼬中间件

这篇关于在使用Upsert更新为true时未设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆