I want to exclude some fields from result. I have code:
users = db.select('users');
users.find( {}, { sort: { points:1 }, privateKey:0, publicKey:0}, function(err,data){      
  res.send(data);
});
I want to exclude private and public key from results. Can I do that using monk?
For me, I need to use the .project() method:
const someFunction = async () => {
    const result = await users
        .find({}, { sort: { points: 1 })
        .project({ privateKey: 0, publicKey: 0});
};
                        According to documentation first argument in find is filter and second is projection .But you have used sort . It will not able to interpret . You are trying to confuse projection with sort .Sorting should be after find and projection.
You can write projection like  { field1: <boolean>, field2: <boolean> ... }
Note : The find() method always includes the _id field even if the field is not explicitly stated to return in the projection parameter.
 users.find({}, { privateKey: 0, publicKey: 0 }).sort({points: 1}).toArray(
           function (err, data) {
                      res.send(data);
             });
                        You can also do it like this:
users.find( {}, { sort: { points:1 }, fields : { privateKey:0, publicKey:0} },
  function(err,data){      
    res.send(data);
  }
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With