I am trying to query a data collection to return just one object from an array of objects using elemMatch. I have this data:
[
{
"_id": "5ba10e24e1e9f4062801ddeb",
"user": {
"_id": "5b9b9097650c3414ac96bacc",
"firstName": "blah",
"lastName": "blah blah",
"email": "blah@gmail.com"
},
"appointments": [
{
"date": "2018-09-18T14:39:36.949Z",
"_id": "5ba10e28e1e9f4062801dded",
"treatment": "LVL",
"cost": 30
},
{
"date": "2018-09-18T14:39:32.314Z",
"_id": "5ba10e24e1e9f4062801ddec",
"treatment": "LVL",
"cost": 30
}
],
"__v": 1
}
]
I need to query the DB and pull out just one appointment depending on the id passed to params. I am using the code below based on the docs here.
router.get(
"/booked/:app_id",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Appointment.find()
.elemMatch("appointments", { id: req.params.app_id })
.then(app => res.json(app));
}
);
This is returning an empty array though when I test with postman. This is the first time I have used node.js and mongoDB so any help would be appreciated. Thanks.