Hi,
I was able to follow https://docs.mongodb.com/v5.0/reference/operator/aggregation/lookup/#examples to create a lookup aggregation between 2 collections. (MongoDB 4.4)
However, when I try to add $sort, for larger datasets like 100K documents, the query times out and no results are returned. If the dataset is small, like 50 documents, it’s fine.
Any suggestions? I followed ESR rule and setup the following:
MongoDB can use the index to support queries on the index prefixes. As such, MongoDB can use the index for queries on the following fields in $match stage/query part:
Case 1:
index({source_id: 1, title: 1})
the source_id field, { "source_id": "123" }
the source_id field and the title field, { "source_id": "123", "title": "search name" }
Case 2:
index({source_id: 1, gid: 1, title: 1})
the source_id field, { "source_id": "123" }
the source_id field and the gid field, { "source_id": "123", "gid": "123" }
the source_id field and the gid field and the title field. { "source_id": "123", "gid": "123", "title": "search name" }
the source_id field and and the title field. { "source_id": "123", "title": "search name" }
In a nutshell, your $lookup doesn’t affect the title field so you should be sorting first and that would be able to use any index that starts with title (or just a single index on title).
Try adding title:1 index to source_products and switch the two agg stages to have $sort first and then $lookup.
I totally forgot the importance of ordering in MongoDB stages. I have since changed the aggregation to the following and added:
search for title via regex, but query can also be null which should show all docs
pagination, both forwards & backwards
index {source_id: 1, title: 1, _id: 1}
I think forward pagination is now working. However, when I try to page backwards, instead of going back 50 documents, it goes back to the start of the collection.
Am I doing something wrong? For example, should the $match be on title & _id?
Does this work in cases if title isn’t unique?
Do I need to specify -1 for sort order for either title or _id?
P.S. The reason why I have 'title' => {'$exists' => true}, is because I need to support paging with
search for specific titles via regex 'title' => /#{query}/i , as well as
search all titles via 'title' => {'$exists' => true}
when I try to page backwards, instead of going back 50 documents, it goes back to the start of the collection
I’m not sure what you are trying to implement - are you talking about the case where the user clicks next page (you fetch next 50 documents greater than last one displayed) and now they click previous page and you need to go back to previous 50? I can’t think of a simple way to do that reliably without keeping "before_cursor" value for previous page(s). I’m sure there is one but they all seem a bit awkward, or involve two queries…
Maybe someone else can think of a simple way to do it in a single query.