如何在猫鼬中结合两个模式?

问题描述 投票:0回答:1

我有一个具有以下两个模式的MongoDB:

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"
    }
}
User:
{
    "_id": {
        "$oid": "5e1f3c9868141f0055208da9"
    },
    "firstName": "Bob",
    "lastName": "Mueller",
    "userName": "bob",
    "email": "[email protected]",
    "salt": "4604426e4ac451654f28025ffe9d09e0",
    "hash": "957eadeef377f9c88082589152b808d6d9611cb5df97db9f15536d9bf47ffd5cad32de916c7cee3dc514a383e7135b645aba7ff75a2365cf2d3ceb2b48415f86679c5ea6bba99a805dcbf9da0c324173d3e2c8d7690120a45122af872c77abf95748df6fa0546db56d88fa0f9055caf6fe78dc4605e774187cc8fa659878402f74ab63765477d6e4590585c018bcbdacd8f363c9b7ce82e2b60700b8f90eaf25b926d4623d1ad9dcb9af2227600ed52175b42cbd4794b6c4aecac11415efc03d2691ae895397117e985172bb0c72242a6ea0d81f26fa936623b97c57c7bfb10bcc2cb2ba39baa2430e584c39f07e34c8e550deae4bf7877bf17d9707392d427e",
    "createdAt": {
        "$date": {
            "$numberLong": "1579105433056"
        }
    },
    "updatedAt": {
        "$date": {
            "$numberLong": "1582223618030"
        }
    },
    "__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填充帮助页面,但实际上并没有成功。

我希望有人可以帮助我,因为我仍然是这个主题的新手。

javascript node.js mongodb mongoose mongoose-populate
1个回答
0
投票

使用subdocuments可以做到这一点。子文档允许您声明单独的模型/模式,然后将它们链接/在其他模式内调用。

这里是文档中的一小段文字:https://mongoosejs.com/docs/subdocs.html

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

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
});
© www.soinside.com 2019 - 2024. All rights reserved.