I have two collections: Consultas
and Pacientes
. I try to get consultations for the patient's name by a term, but was unsuccessful.
var consultaSchema = new Schema({
//detalles de la consulta
paciente_c: { type: Schema.Types.ObjectId, ref: 'Paciente', required: true },
medio_c: { type: String, required: false },
/* ... */
}, { timestamps: true } );
module.exports = mongoose.model('Consulta', consultaSchema);
var pacienteSchema = new Schema({
nombre: { type: String, required: [true, 'El nombre es necesario']},
apellido: { type: String, required: false },
/* ... */
}, { timestamps: true } );
module.exports = mongoose.model('Paciente', pacienteSchema);
And the query:
Consulta.find({})
.populate('paciente_c', 'nombre apellido')
//.or( { paciente_c: { nombre: regex } } )
.or( { 'paciente_c.nombre': regex } )
.exec( (err, consultas )=> {
if (err) {
reject('Error al cargar consultas !!!', err);
} else {
resolve(consultas)
}
});
EDIT: Adding the following line
.populate( 'paciente_c', null, { nombre: { $in: regex } } )
The query brings all the consultations and only populates 'patient_c' in which it finds the term, but it is not what I need. I just want to get the queries in which the term is found and not all.
EDIT 2: The following line work fine, but how it compares between two values?
return new Promise((resolve, reject)=> {
Consulta.find({})
.populate({
path: 'paciente_c',
match: { nombre: { $in: regex } }
})
.exec( (err, consultas )=> {
if (err) {
reject('Error al cargar consultas !!!', err);
} else {
consultas = consultas.filter(c => c.paciente_c != null);
resolve(consultas)
}
});
});