簡體   English   中英

如何在 Mongoose 中組合兩個模式?

[英]How do I combine two Schemas in Mongoose?

我有一個具有以下兩個架構的 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": "bob@bob.com",
    "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();

正如您所看到的,我為用戶和調查提供了單獨的模型,但我還想知道如何將它們結合起來。 我曾嘗試使用貓鼬填充幫助頁面,但它並沒有真正奏效。

我希望有人能幫助我,因為我對這個話題還是陌生的。

這可以使用subdocuments 子文檔允許您聲明單獨的模型/模式,然后在其他模式中鏈接/調用它們。

這是文檔中的一些文字: https : //mongoosejs.com/docs/subdocs.html

子文檔是嵌入在其他文檔中的文檔。 在 Mongoose 中,這意味着您可以將模式嵌套在其他模式中。 Mongoose 有兩個不同的子文檔概念:子文檔數組和單個嵌套子文檔。

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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM