user2180794
user2180794

Reputation: 1445

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 ?

Upvotes: 0

Views: 31

Answers (1)

ZachB
ZachB

Reputation: 15366

Use the $exists operator:

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

Upvotes: 1

Related Questions