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);