Alberto
Alberto

Reputation: 399

Mongoose $ project

Using Mongoose 4.0.x, I need to execute the following (working) MongoDB query:

db.bookings.find(
  {
    user: ObjectId("10"), // I replaced the real ID
    'flights.busy.from': {$gte: ISODate("2015-04-01T00:00:00Z")},
    'flights.busy.to': {$lte: ISODate("2015-04-01T23:59:00Z")}
  },
  {
    'flights.$': 1 // This is what I don't know to replicate
  }
).pretty()

The Mongoose find operator does not accept a projection operator, like the MongoDB find one does.

How can I replicate the above query in Mongoose? Filtering the array once the query is returned is a solution I would like to avoid.

Upvotes: 0

Views: 319

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311855

You want to look at the docs for Model.find, not Query.find. The second parameter can be used for field selection:

MyModel.find(
  {
    user: ObjectId("10"), // I replaced the real ID
    'flights.busy.from': {$gte: ISODate("2015-04-01T00:00:00Z")},
    'flights.busy.to': {$lte: ISODate("2015-04-01T23:59:00Z")}
  },
  'flights.$'
).exec(function(err, docs) {...});

Upvotes: 2

Related Questions