user2180794 user2180794 - 6 months ago 9
Node.js Question

How to find all documents with a particular filed

I have the following code, but this is pulling all the documents and showing the two mentioned fields. But I want to get only those documents which have these fields.

.get(function(req, res) {
partment.find({},'unitID UnitType',function(err, apartments) {
if (err)
res.send(err)
res.json(apartments);
});
}


Any Idea how I can do that ?

Answer

Use the $exists operator:

payment.find({unitId: {$exists: true}, UnitType: {$exists: true}}, ...)