I have a mongoose schema:
var ClientRentalSchema = new mongoose.Schema({
carId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Car'
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
startDate: Date,
endDate: Date,
payment: Number,
status: { type: String, default: "Oczekujące" },
discount: { type: Number, default: 0 }
});
Now, I'm trying to find an object by the user's ID card number. Firstly the closest I got is by this piece of code:
Rental.find()
.populate('userId', 'idNumber')
.findOne({'userId.idNumber': 'XYZ 987654'})
.exec( (err, values) => {
if(err) return err;
res.json(values);
})
It gives a null. This is a sample rental object after popluating:
{"status":"Aktywne",
"discount":0.2,
"_id":"5ae06b49abafb90e20359892",
"carId":"5ad6186ebd371940e4131f71",
"userId":{"_id":"5ad8e077a59b7d58fc2384bd",
"idNumber":"ABC 123456"},
"startDate":"2018-04-26T09:11:00.000Z",
"endDate":"2018-04-29T09:11:00.000Z",
"payment":237.6,
"__v":0}
What would be the proper way of searching for a rental by the users id card number?