How popular is an object loaded with populate in Mongoose?

Asked

Viewed 62 times

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?

1 answer

1


You would have to use populate for multiple levels. In your case you can try to pass a contentious array of field rules to popular.

An example for your case, let’s popular the field reforco and from this field we also want to popular the field operador:

Os.find({ nome: req.body.nome })     // filtro qualquer
  .populate({
    path: 'reforco',                 // populamos o "reforco"
    populate: [{ path: 'operador' }] // passamos um array com objetos
                                     // com os campos a serem populados
                                     // neste caso o campo "operador"
  })

If you want to populate more fields, pass more objects to the array:

Os.find({ nome: req.body.nome })
  .populate({
    path: 'reforco',
    populate: [{ path: 'operador' }, { path: 'pragas' }, { path: 'produtos' }]
  })

It’s called Populating Across Multiple levels and we can find in the documentation of Mongoosis.

  • This works on as many levels as necessary right?

  • 1

    yes, but stay tuned for the issue of performance because popular various fields, can affect the performance of the query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.