Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Javascript 如何在Mongoose中组合两个模式?_Javascript_Node.js_Mongodb_Mongoose_Mongoose Populate - Fatal编程技术网

Javascript 如何在Mongoose中组合两个模式?

Javascript 如何在Mongoose中组合两个模式?,javascript,node.js,mongodb,mongoose,mongoose-populate,Javascript,Node.js,Mongodb,Mongoose,Mongoose Populate,我有一个MongoDB和以下两个Schmema: Survey: { "_id": { "$oid": "5e4ed11c73c4c900ef824b8a" }, "isRunning": false, "title": "TestSurvey", "dateOfCreation": { "$date": { "$numberLong": "1582223644144" } }

我有一个MongoDB和以下两个Schmema:

Survey:
{
    "_id": {
        "$oid": "5e4ed11c73c4c900ef824b8a"
    },
    "isRunning": false,
    "title": "TestSurvey",
    "dateOfCreation": {
        "$date": {
            "$numberLong": "1582223644144"
        }
    },
    "dateOfExpiration": {
        "$date": {
            "$numberLong": "1592438400000"
        }
    },
    "isPublic": true,
    "user": {
        "$oid": "5e1f3c9868141f0055208da9"
    },
    "maxParticipants": {
        "$numberInt": "50"
    },
    "questions": [],
    "__v": {
        "$numberInt": "0"
    }
}
我的目标是制作一个列表,在其中我可以看到所有公开的调查(isPublic:true)、创建调查的用户的用户名以及调查的截止日期。为了实现这一点,我需要在调查模式中使用userId来引用用户

在我的项目中,我有以下控制器:

const {Survey} = require('../../models');
const {User} = require('../../models');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

class HomeController {

  async homeView(req, res, next) {
    try {
      const survey = await Survey.findOne();
      const user = await User.findOne();

      const userSchema = new Schema({
        name: String,
        _id: Schema.ObjectId,
      })

      const surveySchema = new Schema({
        title: String,
        expirationDate: Date,
        creator: [userSchema]
      })

      const surveys = mongoose.model('surveys', surveySchema);

      res.render('home/homeView', {
        title: 'All surveys',
        surveys,
    });
    } catch (err) {
      next(err);
    }

  }
}

module.exports = new HomeController();
正如你所看到的,我为用户和调查提供了单独的模型,但我还不知道如何将它们结合起来。我尝试过使用Mongoose填充帮助页面,但没有真正起作用


我希望有人能帮助我,因为我对这个话题还是新手。

这是通过使用
子文档实现的。子文档允许您声明单独的模型/模式,然后在其他模式中链接/调用它们

以下是文档中的一些文字:

子文档是嵌入在其他文档中的文档。在猫鼬中, 这意味着您可以在其他模式中嵌套模式。猫鼬有两个 子文档的不同概念:子文档数组和单个 嵌套子文档


我是否需要在控制器或为用户和调查声明模式的文件中执行此操作?您可以在一个文件中声明子模式,然后像往常一样导出。使用
require
命令导入架构,然后使用架构名称作为
type
属性。
const {Survey} = require('../../models');
const {User} = require('../../models');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

class HomeController {

  async homeView(req, res, next) {
    try {
      const survey = await Survey.findOne();
      const user = await User.findOne();

      const userSchema = new Schema({
        name: String,
        _id: Schema.ObjectId,
      })

      const surveySchema = new Schema({
        title: String,
        expirationDate: Date,
        creator: [userSchema]
      })

      const surveys = mongoose.model('surveys', surveySchema);

      res.render('home/homeView', {
        title: 'All surveys',
        surveys,
    });
    } catch (err) {
      next(err);
    }

  }
}

module.exports = new HomeController();
var childSchema = new Schema({ name: 'string' });

var parentSchema = new Schema({
  // Array of subdocuments
  children: [childSchema],
  // Single nested subdocuments. Caveat: single nested subdocs only work
  // in mongoose >= 4.2.0
  child: childSchema
});