【问题标题】:Copy _id to new Field while creating new documents (save)创建新文档时将_id复制到新字段(保存)
【发布时间】:2020-05-26 07:04:30
【问题描述】:

我在 Mongoose 中有这个架构

ProfileEmailSchema = module.exports = mongoose.Schema({
    username: {
        type: String,
        unique: true,
        index: true,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    profile: { type: Number, unique: true, required: true, index: true },
    email: {
        type: String,
        required: true,
        unique: true,
        index: true
    },
    fullname: {
        type: String,
        required: true

    },
    display_picture: {
        type: String
    },
    isProfileCompleted: {
        type: Boolean,
        deafult: false
    },
    profile: created_at: { type: Date, default: Date.now },
    updated_at: { type: Date, default: Date.now }
});


ProfileEmailSchema.pre('save', function(next) {
    log("Saving  Profile Data :");
    now = new Date();
    this.updated_at = now;
    if (!this.created_at) {
        this.created_at = now
    }
    next();
});


ProfileEmailSchema.pre("save", function(next) {
    var user = this;

    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next();

    // generate a salt
    bcrypt.genSalt(10, function(err, salt) {
        if (err) return next(err);

        // hash the password using our new salt
        bcrypt.hash(user.password, salt, function(err, hash) {
            if (err) return next(err);

            // override the cleartext password with the hashed one
            user.password = hash;
            next();
        });
    });

});


// test Function
ProfileEmailSchema.methods.find = function(cb) {
    this.model('ProfileEmailModel').findOne({}, cb);
};

//Pass Comparison Function
ProfileEmailSchema.methods.comparePassword = function(password, cb) {
    log("Compare Password with HASHED pass");
    log(password);
    log("HASHED");
    log(this.password);
    bcrypt.compare(password, this.password, function(err, isMatch) {
        if (err) return cb(err);

        log("Return Status:");
        log(isMatch);
        cb(null, isMatch);
    });
};

ProfileEmailModel = module.exports = mongoose.model("ProfileEmailModel", ProfileEmailSchema);

我面临的问题是我需要在执行以下操作时将 _id 复制到配置文件字段

 var tuple = new UserProfileModel({
                    username: profile.username,
                    email: profile.email,
                    fullname: profile.fullname,
                    password: profile.password,
                });

 console.log(tuple);

我正在尝试这样使用但无济于事

 var tuple = new UserProfileModel({
                username: profile.username,
                email: profile.email,
                fullname: profile.fullname,
                password: profile.password,
                profile :  mongoose.Schema.Types.ObjectId
            });

 console.log(tuple);

但它不工作。在使用tuple.save() 创建第一个文档时,我需要确保在创建新文档时将_id 复制到profile 键。

请建议。否则我将需要在应用程序中进行更改,这将是 4 个月。

【问题讨论】:

  • 所以,如果 _id 是由 MongoDB 在插入时创建的,您不能将它们复制到 profile,而是可以在您的客户端创建 _id 并将其复制到 profile然后保存..

标签: javascript mongodb mongoose mongodb-query mongoose-schema


【解决方案1】:

我找到了更好的解决方案。使用了pre 钩子

  UserProfileSchema.pre('save', function(next) {

    console.log("#####################");
    console.log("Saving  New Document :");
    now = new Date();
    this.updated_at = now;
    if (!this.created_at) {
        this.created_at = now
    }
    console.log("Login Type :");
    console.log(this.login_type);
    if (this.login_type == 'email') {
        console.log("Saving _id To Profile");
        console.log("ID :" + this._id);
        var ID = this._id;
        this.profile = ID;
        console.log(" Profile :");
        console.log(this.profile);
    }

    next();
});

这是在保存数据的同时复制_id 的简单方法。我使用了 mongoose 提供的 PRE 钩子。我得到_id 并分配给profile

这解决了我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2015-02-09
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多