This is Meal schema .I want to get object inside meal array by filtering meal.owner for specific user.
what will be the query? .
can anybody help me please.
This is Meal schema .I want to get object inside meal array by filtering meal.owner for specific user.
what will be the query? .
can anybody help me please.
What code have you tried so far?
Meal.find({'meal.owner':req.params.memberId})
I am very new in mognoDB . I just tried above query but it gives me all data .
Please help me .
Are you using Mongoose?
Yes sir i am using mongoose.
Since meal.owner is an ObjectId type, you can not just query on a string value like req.params.memberId
. You can use the following code which convertx the string to an ObjectId type to use in the query.
const ObjectId = require('mongoose').Types.ObjectId;
Meal.find({'meal.owner': new ObjectId(req.params.memberId)});
You need to make sure to use some validation that req.params.memberId
has a value or you will get an error.
const ObjectId = require('mongoose').Types.ObjectId;
Meal.find({'meal.owner': new ObjectId(req.params.memberId)});
Sir i tried this query but it still gives me all the data .please sir help me …