Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
How do I perform a search referenced models with mongoose?

I have up 2 models "car" and "carcheck" . Car has plate and manufacturer, model as model items and carcheck has its own items plus a reference to the car model. How is it possible to create a Search Query for carcheck model that will look also into the reference items of plate, manufacturer and model?

Searching for plate manufacturer or model straight away just returns an empty array.

const defineSearchQueryForCarCheck = (req)=>{
    return req.query.search ? {
        $or:[
            {plate: {$regex:req.query.search, $options: "i"}},
            {manufacturer: {$regex:req.query.search, $options: "i"}},
            {model: {$regex:req.query.search, $options: "i"}},
            {milage: {$regex:req.query.search, $options: "i"}}
        ],           
 
    }: {}
}

The above code is the one that returns the empty array.

I also tried car.plate, car.manufacturer and so on, or car[plate] etc. but they do not seem to be working.

the model for the carCheck is:

const mongoose = require("mongoose");

const carCheckSchema = new mongoose.Schema({
    car: {type: mongoose.ObjectId,ref:"Car",required:true},
    milage: {type:String, required:true},
    condition: {type:String, required:true},
    conditionComments: {type:String},
    carCheckedStatus: {type:Boolean, default:false},
    }, {
        timestamp : true,
});

module.exports = mongoose.model("CarCheck", carCheckSchema);

and the car model is the below code:

const mongoose = require("mongoose");

const carsSchema = new mongoose.Schema({
    plate: {type:String, required:true, unique: true},
    manufacturer: {type:String, required:true},
    model: {type:String, required:true},
    }, {
        timestamp : true,
});

module.exports = mongoose.model("Car", carsSchema);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!