Find object id in object ids array returns empty array using Mongoose Find object id in object ids array returns empty array using Mongoose mongoose mongoose

Find object id in object ids array returns empty array using Mongoose


In your EmployeeSchema, servicesProvided is an array, to filter employees by that field you should use $in operator:

var services = [req.params.service_id];Employee.find({  servicesProvided: {    $in: services  }}, ...


I think you need $elemMatch! From docs:

{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] },{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] },{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }

Search like:

db.survey.find({ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } })

Results in:

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }

But since you're doing a single query condition (look at the docs again) you can replace

db.survey.find(   { results: { $elemMatch: { product: "xyz" } } })

with

db.survey.find(   { "results.product": "xyz" })

So in your case it should be something like:

find({    'servicesProvided': ObjectId(req.params.service_id)})