【发布时间】:2022-01-25 06:57:41
【问题描述】:
我有一个文件模型引用了一个笔记模型,而该笔记引用了一个问题模型。
一个文件可以有很多注释,不同的注释可以有很多问题,如何在MongoDB中通过文件ID查询文件,返回文件下所有注释下的所有问题。
文件--->注释--->问题
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const fileSchema = new Schema(
{
title: {
type: String,
required: [true, 'Please add a title'],
trim: true,
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
notes: [
{
type: mongoose.Schema.ObjectId,
ref: 'Note',
},
],
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
},
);
module.exports = mongoose.model('File', fileSchema);
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const noteSchema = new Schema(
{
title: {
type: String,
required: [true, 'Please add a title'],
trim: true,
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
questions: [
{
type: mongoose.Schema.ObjectId,
ref: 'Question',
},
],
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
},
);
【问题讨论】:
标签: javascript node.js mongodb mongoose-schema