Answer the question
In order to leave comments, you need to log in
How to write an aggregation with a population of data for the following model?
There are 2 models (Post and Author) that are passed using refPath to Data:
Data model:
import { model, Schema, Types } from 'mongoose';
const DataSchema = new Schema({
page: { type: Types.ObjectId, ref: 'Page', required: true },
instance: {
type: Schema.Types.ObjectId,
required: true,
// Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
// will look at the `onModel` property to find the right model.
refPath: 'fields'
},
fields: {
type: String,
required: true,
enum: ['Post', 'Author']
}
});
export const Data = model('Data', DataSchema);
import { model, Schema, Types } from 'mongoose';
const PostSchema = new Schema({
title: String
});
export const Post = model('Post', PostSchema);
import { model, Schema, Types } from 'mongoose';
const AuthorSchema = new Schema({
name: String
});
export const Author = model('Author', AuthorSchema);
const allData = await Data.find({ page: { $in: [...pages] } }).populate(['instance', 'page']);
[{страница, дата этой сраницы}, {страница, дата этой сраницы}, {страница, дата этой сраницы}]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question