soulpaul
soulpaul

Reputation: 172

Mongoose query on date intervals

In my Mongoose Schema "photographer" i have a property with nested objects that describes when the photographer is not available, made as follows:

const photographer = new Schema({
  _id : Schema.Types.ObjectId,
  ...
  busy: [{startDate : Date, endDate : Date}],
  ...
  })

I need to query all the documents, given a date, that represent photographers free in a that given time. I don't know how to write the right query. Can someone please help me?

Upvotes: 1

Views: 527

Answers (1)

Khay
Khay

Reputation: 1570

You may want to make a range query. Switch $gt and $lt depends on you program. For example:

var myDate = new Date(req.query.date); // 
Photographer.find({
    $and: [
        {'busy.startDate': {$lt: myDate}},
        {'busy.endDate': {$gt: myDate}}
    ]
}, (err, photographers) => {})

Or, simple way is get all photographers first, and then make conditions to check whether a photographer is busy on provided day or not.

Upvotes: 1

Related Questions