Reputation: 1088
I am trying to filter a mongoDB documents based on specific properties. Here's what i'm trying to do. lets say i have a user doc and within this User doc i have like 10 fields. out of these 10 fields i want to filter my query to get just about 4.
i tried something like this
UserDoc.find({}, email:1, dob:1, hasInsurance:1, isMarried:1)
This is assuming that i only want the query to return these specific fields. I saw this approach here on SO but doesn't seem to work. could anyone point me in the right direction?
Upvotes: 0
Views: 101
Reputation: 185
In nodejs you can use below syntax to get specific filed in output-
UserDoc.find({}).select("email").select("dob").select("hasInsurance").select("isMarried")
You can remove specify filed from output . It will only remove email filed from the output-
UserDoc.find({}).select("-email")
Upvotes: 1
Reputation: 363
in the mongo shell try:
UserDoc.find({}, {email:1, dob:1, hasInsurance:1, isMarried:1})
and with mongoose in nodejs use:
UserDoc.find({}).select("email dob hasInsurance isMarried")
Upvotes: 2