How to exclude one particular field from a collection in Mongoose?

前端 未结 6 2159
逝去的感伤
逝去的感伤 2020-12-13 12:15

I have a NodeJS application with Mongoose ODM(Mongoose 3.3.1). I want to retrieve all fields except 1 from my collection.For Example: I have a collection Pr

6条回答
  •  臣服心动
    2020-12-13 12:50

    In the updated version of Mongoose you can use it in this way as below to get selected fields.

    user.findById({_id: req.body.id}, 'username phno address').then(response => {
      res.status(200).json({
        result: true,
        details: response
      });
    }).catch(err => {
      res.status(500).json({ result: false });
    });
    

提交回复
热议问题