Skip to content

Latest commit

 

History

History
99 lines (88 loc) · 2.37 KB

QUERY.md

File metadata and controls

99 lines (88 loc) · 2.37 KB

Schedule Query Collection

Sample Model

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'),
    },
  ],
};

Sample Query

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);

Indexing

Query Index

{
  "schedules.sectionId" : 1,
  "schedules.optionId" : 1
}
// Unique
{
  "contentId": 1
}

Sort Index

{
  "schedules.0.start" : -1,
  "_id": -1
}

Note: The _id field must be added to the short index due to how pagination works.

When Hooks Fire

When a hook is fired, all query entries for the related content ID will be removed and then all entries will be reinserted.

Content

Anytime a content model is created or updated.

Will insert query entries based on:

  • status
  • primaryImage
  • published
  • unpublished

Schedules

Anytime a schedule model is created, updated, or deleted.

Will insert query entries based on:

  • product
  • section
  • option
  • startDate
  • endDate

Notes

  • The end field is calculated as follows:
    • If schedule.endDate exists use it if, and only if, it's less than or equal to content.unpublished or content.unpublished is null.
    • If schedule.endDate does not exist, use the value of content.unplublished (which could be null)
  • The start field is calculated as follows:
    • If schedule.startDate exists use it if, and only if, it's greater than or equal to content.published; otherwise use the content.published value
    • If content.published is not set, then a query entry should not be added to the database.