0
I have a doubt, if I already have a query being populated in Mongoose, and it is populated with a model that also has relationship, there is a way to popular it too?
For example:
I have my model of Os
:
const mongoose = require('../config/database');
const Schema = mongoose.Schema;
const OsSchema = new Schema({
nome: {type: String, required: true},
cnpjcpf: {type: String, required: true},
dataservico: { type:Date, required:true},
datagarantia: { type:Date, required:true},
duracao: { type:Number},
dificuldade: {type: Number},
vendedor: {type: String, required: true},
score: {type: Number, required: true},
done: {type: Boolean, required:true},
created: { type:Date, default:Date.now()},
operador:[{type: Schema.Types.ObjectId, ref: 'Operador'}],
pragas:[{type: Schema.Types.ObjectId, ref: 'Praga'}],
produtos:[{type: Schema.Types.ObjectId, ref: 'Produto'}],
reforco:[{type: Schema.Types.ObjectId, ref: 'Reforco'}],
},{toJSON:{virtuals:true}});
module.exports = mongoose.model('Os', OsSchema);
But my model of reforco
also has relations:
const mongoose = require('../config/database');
const Schema = mongoose.Schema;
const ReforcoShema = new Schema({
dataservico: { type:Date, required:true},
duracao: { type:Date},
done: {type: Boolean, required:true},
programado: {type: Boolean, required:true},
operador:[{type: Schema.Types.ObjectId, ref: 'Operador'}],
pragas:[{type: Schema.Types.ObjectId, ref: 'Praga'}],
produtos:[{type: Schema.Types.ObjectId, ref: 'Produto'}],
created: { type:Date, default:Date.now()}
},{toJSON:{virtuals:true}});
module.exports = mongoose.model('Reforco', ReforcoShema);
When I query Os
and population the reforco
, He brings back the reinforcement normally, but only brings back the ID
of the relations of the reinforcement, there is a means of popular they also?
This works on as many levels as necessary right?
– Kai
yes, but stay tuned for the issue of performance because popular various fields, can affect the performance of the query.
– Cmte Cardeal