Exclude fields from result in MongoDB monk

前端 未结 2 469
天涯浪人
天涯浪人 2021-01-11 19:18

I want to exclude some fields from result. I have code:

users = db.select(\'users\');

users.find( {}, { sort: { points:1 }, privateKey:0, publicKey:0}, func         


        
相关标签:
2条回答
  • 2021-01-11 19:56

    You can also do it like this:

    users.find( {}, { sort: { points:1 }, fields : { privateKey:0, publicKey:0} },
      function(err,data){      
        res.send(data);
      }
    );
    
    0 讨论(0)
  • 2021-01-11 20:10

    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);
                 });
    
    0 讨论(0)
提交回复
热议问题