Mongoose.js using find({...}, -some-field)

Can someone link to where I can find a reference or documentation for excluding a field from the find() query when using mongoose in Node.js? Like so:

Exercise.find({
      userId: req.query.userId,
      date: { $gte: req.query.from || minDate},
      date: { $lte: req.query.to || maxDate}
}, "-_id")

This excludes the “_id” field but I cannot find what this is called. I only found a solution on Stack Overflow. Thanks.

Hello @rhruby2, welcome to FCC forum. I believe mongoose documentation about find method might be helpful. I have never used mongoose of late but I believe what you want is how to pass the projection optional argument to specifically exclude certain fields. If adding "name friends" will return name and friends fields, then I believe "-name -friends" will exclude them. There are quite a number of different ways you can pass the projection parameter (That is what it is called ) to Model.find in mongoose. Find more from the official mongoose documentation linked above and perhaps the article below.

@nibble Thanks for the reply.
Query Projection. Some things notable for those interested.

It seems the usage from the mongoose documentation:

// executes, name LIKE john and only selecting the "name" and "friends" fields
await MyModel.find({ name: /john/i }, 'name friends').exec();

Does only include those fields PLUS the unique key, which I do not want.
So if I did something like

Exercise.find({
      userId: req.query.userId,
      date: { $gte: req.query.from || minDate},
      date: { $lte: req.query.to || maxDate}
    }, 'description duration date')

the field “_id” is still included.

The working solution for me was from the other linked article using:

// Explicitly exclude `email` using the 2nd argument. Use `email: 1` to
// include _only_ the `email` property.
Customer.find({}, { email: 0 });

for my use case:

Exercise.find({
      userId: req.query.userId,
      date: { $gte: req.query.from || minDate},
      date: { $lte: req.query.to || maxDate}
    },{_id: 0})

which did absolutely exclude the “_id” field. I still do not understand where that syntax is in the Mongoose documentation on Query Projection is though.

EDIT: Found it! This syntax is in the Query select() section:

Mongoose v5.11.8: API docs (mongoosejs.com)