I’m implementing paging/sorting on a multi-tenant collection. Any reasonable filter on top of the full data set where results are in the tens or low thousands performs very will. Worst case is there is no filter (other than tenantId filter that gets added automatically). In this case, 750k total documents takes 43 seconds. I’m happy to provide more info or get on a call to discuss more, but figured i’d post what i have collected so far.
db.mycollection.aggregate(
[
{
$search: {
index: "default",
returnStoredSource: true,
count: {
type: "total",
},
compound: {
must: [
{
text: {
query: "60140fae076eeb001176245c",
path: "tenantId",
},
},
],
},
},
},
{
$sort: { displayName: 1 },
},
{
$facet: {
totalCount: [
{ $limit: 1 },
{ $project: { meta: "$$SEARCH_META" } },
{ $group: { _id: null, count: { $sum: "$meta.count.total" } } },
],
results: [
{ $skip: 0 },
{ $limit: 10 },
{
$lookup: {
from: "mycollection",
localField: "_id",
foreignField: "_id",
as: "document",
},
},
{
$unwind: {
path: "$document",
},
},
{
$replaceRoot: {
newRoot: "$document",
},
},
],
},
},
],
{
allowDiskUse: true,
}
);