mongodb 如何在我的Mongoose模式中引用另一个模式?

nx7onnlm  于 2022-12-18  发布在  Go
关注(0)|答案(3)|浏览(127)

我在为一个约会应用做 Mongoose 模式。
我希望每个person文档都包含一个对他们去过的所有事件的引用,其中events是另一个模式,在系统中有自己的模型。

var personSchema = mongoose.Schema({
    firstname: String,
    lastname: String,
    email: String,
    gender: {type: String, enum: ["Male", "Female"]}
    dob: Date,
    city: String,
    interests: [interestsSchema],
    eventsAttended: ???
});
bvuwiixz

bvuwiixz1#

您可以使用**Population**执行此操作
填充是将文档中的指定路径自动替换为其他集合中的文档的过程。我们可以填充单个文档、多个文档、普通对象、多个普通对象或查询返回的所有对象。
假设您的事件架构定义如下:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema

var eventSchema = Schema({
    title     : String,
    location  : String,
    startDate : Date,
    endDate   : Date
});

var personSchema = Schema({
    firstname: String,
    lastname: String,
    email: String,
    gender: {type: String, enum: ["Male", "Female"]}
    dob: Date,
    city: String,
    interests: [interestsSchema],
    eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
});

var Event  = mongoose.model('Event', eventSchema);
var Person = mongoose.model('Person', personSchema);

为了展示如何使用populate,首先创建一个person对象,
aaron = new Person({firstname: 'Aaron'})和事件对象,
event1 = new Event({title: 'Hackathon', location: 'foo'})

aaron.eventsAttended.push(event1);
aaron.save(callback);

然后,当您进行查询时,可以按如下方式填充引用:

Person
.findOne({ firstname: 'Aaron' })
.populate('eventsAttended') // only works if we pushed refs to person.eventsAttended
.exec(function(err, person) {
    if (err) return handleError(err);
    console.log(person);
});

vfh0ocws

vfh0ocws2#

要在一个表中引用另一个表的ObjectId,请参考以下代码

const mongoose = require('mongoose'),
Schema=mongoose.Schema;

const otpSchema = new mongoose.Schema({
    otpNumber:{
        type: String,
        required: true,
        minlength: 6,
        maxlength: 6
    },
    user:{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }
});

const Otp = mongoose.model('Otp',otpSchema);

// Joi Schema For Otp
function validateOtp(otp) {
    const schema = Joi.object({
        otpNumber: Joi.string().max(6).required(),
        userId: Joi.objectId(),   // to validate objectId we used 'joi-objectid' npm package
        motive: Joi.string().required(),
        isUsed: Joi.boolean().required(),
        expiresAt: Joi.Date().required()
    });
    // async validate function for otp
    return schema.validateAsync(otp);
}

exports.Otp = Otp;
exports.validateOtp = validateOtp;
2fjabf4q

2fjabf4q3#

列表项

var personSchema = mongoose.Schema({
    firstname: String,
    lastname: String,
    email: String,
    gender: {
        type: String,
        enum: ["Male", "Female"]
    }
    dob: Date,
    city: String,
    interests: [interestsSchema],
    eventsAttended[{
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: "Place"
    }], 
**//ref:"Places"...you have put the other model name**
 *OR*
    eventsAttended[{
        type: mongoose.Types.ObjectId,
        required: true,
        ref: "Place"
    }],
});

相关问题