const model = {
contentId: 20994010,
contentType: 'Product',
hasImage: true,
created: ISODate('2018-09-14T11:22:32.109-05:00'),
// The schedules array *must* be sorted by start, descending so the most recent schedule appears first in the list.
schedules: [
{
sectionId: 56305,
optionId: 2,
start: ISODate('2018-01-16T12:43:13.000-05:00'),
},
{
sectionId: 56298,
optionId: 2,
start: ISODate('2018-01-16T12:43:11.000-05:00'),
end: ISODatE('2018-12-31T12:43:11.000-05:00'),
},
],
};
db.getCollection('SectionQueryArray').find({
schedules: {
$elemMatch: {
sectionId: { $in: [56403, 56208] },
optionId: 2,
start: { $lte: new Date() },
$and: [
{
$or: [
{ end: { $gt: new Date() } },
{ end: { $exists: false } },
],
},
],
},
},
}, { contentId: 1, 'schedules.$.start': 1 }).sort({ 'schedules.0.start': -1 }).limit(20);
{
"schedules.sectionId" : 1,
"schedules.optionId" : 1
}
// Unique
{
"contentId": 1
}
{
"schedules.0.start" : -1,
"_id": -1
}
Note: The _id
field must be added to the short index due to how pagination works.
When a hook is fired, all query entries for the related content ID will be removed and then all entries will be reinserted.
Anytime a content model is created or updated.
Will insert query entries based on:
status
primaryImage
published
unpublished
Anytime a schedule model is created, updated, or deleted.
Will insert query entries based on:
product
section
option
startDate
endDate
- The
end
field is calculated as follows:- If
schedule.endDate
exists use it if, and only if, it's less than or equal tocontent.unpublished
orcontent.unpublished
is null. - If
schedule.endDate
does not exist, use the value ofcontent.unplublished
(which could be null)
- If
- The
start
field is calculated as follows:- If
schedule.startDate
exists use it if, and only if, it's greater than or equal tocontent.published
; otherwise use thecontent.published
value - If
content.published
is not set, then a query entry should not be added to the database.
- If