Reputation: 172
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
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