Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在我的Mongoose模式中引用另一个模式?_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何在我的Mongoose模式中引用另一个模式?

Node.js 如何在我的Mongoose模式中引用另一个模式?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在为约会应用程序建立猫鼬模式 我希望每个person文档都包含对他们所经历的所有事件的引用,其中events是系统中具有自己模型的另一个模式。我如何在模式中描述这一点 var personSchema = mongoose.Schema({ firstname: String, lastname: String, email: String, gender: {type: String, enum: ["Male", "Female"]} dob: D

我正在为约会应用程序建立猫鼬模式

我希望每个
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: ???
});

您可以使用

填充是自动替换指定对象的过程 文档中包含来自其他集合的文档的路径。我们 可以填充单个文档、多个文档、普通对象、, 多个普通对象,或从查询返回的所有对象

假设您的事件模式定义如下:

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);
要显示如何使用填充,首先创建一个person对象,
aaron=newPerson({firstname:'aaron'})
和一个事件对象,
event1=newevent({title:'Hackathon',location:'foo'})

然后,在进行查询时,可以像这样填充引用:

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);
});
要在另一个表中引用一个表的ObjectId,请参阅下面的代码
类似这样,我相信:
eventsAttended:[{type:app.mongoose.Schema.ObjectId,ref:'Event'}]
我能问你一个问题吗?如果我还想在
事件
模式中列出
与会者
,该怎么办?这会导致循环问题吗?您可以在两个方向上同时创建引用,而无需任何可能的循环依赖项。Mongoose文档中的示例很好地解释了这一点。eventsAttended的类型脚本是什么?
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);
});
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;