在mongodb中更新模型时更新参考模型

时间:2018-09-23 04:40:51

标签: mongodb object-reference

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var userSchema = new mongoose.Schema({
username: {type: String, unique: true, required: true},
password: String,
firstname: String,
lastname: String,
email: {type: String, unique: true, required: true},
avatar: String,
resetPasswordToken: String,
resetPasswordExpires: Date
});

userSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", 
userSchema)
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
price: String,
createdAt: {type: Date, default: Date.now},
description : String,
author: {
    id:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
    username:String
},
comments : [
{
    type: mongoose.Schema.Types.ObjectId,
    ref: "comment"
}
]
}); 

module.exports = mongoose.model("Campground", campgroundSchema);

我有这两个模式。当我尝试更新用户详细信息时,仅更改了用户模型,但未更改营地中的用户详细信息(由ID引用)。如何在所有引用的模型中更新用户详细信息?

0 个答案:

没有答案