How can I do a find to search on an ObjectID in mongoose?

1

For example I have two models in mongoose:

var Persona = {
   nombre: "Luis",
   carros:{
     type: Schema.ObjectId,
     ref:'Carro'
   },
  apellidos:"Colx Redex"
}

var Carro = {
  modelo:"FX-200",
  velocidad:"300km/h",
  marca:"Nissan"
}

Then how do I search the person model but also include the car model ... example:

Persona.find({persona.carros.marca: "Nissan"}, function(err, docs){

});

When what is actually stored in the Persona model is only the ObjectId ("5b93f9ee75323d09084af622") of the car ....

I hope any help, thank you very much !!

    
asked by Angel Loera Zamora 08.09.2018 в 22:15
source

1 answer

0

You can try doing it:

Persona.find().populate({
path: 'Carro',
match: {marca: 'Nissan'}
})
.exec()
.then(resultado => console.log(resultado))
.catch(err => console.log(err));
    
answered by 12.09.2018 в 08:57