thekid / dialog

Dialog photoblog
2 stars 1 forks source link

Optimize Repository::entries() once MongoDB Atlas is running 5.2+ #52

Closed thekid closed 1 year ago

thekid commented 1 year ago

See comment in Repository::entries():

...
$cursor= $entries->aggregate([
  ['$match' => ['parent' => ['$eq' => null], 'published' => ['$lt' => Date::now()]]],
  ['$unset' => '_searchable'],
  ['$sort'  => ['date' => -1]],
  ['$skip'  => $pagination->skip($page)],
  ['$limit' => $pagination->limit()],
]);

// We could use $lookup here but would then not have the children sorted
// properly - $sortArray is not available until 5.2, and Atlas free tier
// currently runs 5.0. TODO: Change this once prerequisites are fully met!
$results= [];
foreach ($cursor as $entry) {
  if (empty($entry['images'])) {
    $children= $entries->aggregate([
      ['$match' => ['parent' => $entry['slug']]],
      ['$unset' => '_searchable'],
      ['$sort'  => ['date' => -1]],
      ['$limit' => $children],
    ]);
    $entry['children']= $children->all();
  }
  $results[]= $entry;
}
...
thekid commented 1 year ago

Just got this email yesterday:

image

thekid commented 1 year ago

At the time of writing, M0 clusters are still at version 5.0:

Screenshot

thekid commented 1 year ago

Finally :)

image
thekid commented 1 year ago

Bonus: Figure out if there is a way to conditionally perform the lookup stage as done in the code above

Looks like https://stackoverflow.com/questions/53711144/conditional-lookup-in-mongodb might be a solution

thekid commented 1 year ago

Released in https://github.com/thekid/dialog/releases/tag/v1.15.0