Aggregation, limiting by returned document amount

Hi Everyone

Currently doing an aggregation where we use $facet to run multiple pipelines at once to create an object. However, I want to limit/filter so for example in the $match stage if at least 10 documents are not returned we stop this pipeline.

I have put my query below and explain it. Also if there is a better way to do this let me know. (I am using the Node driver)

const similar = await db
					$facet: entities.reduce((total, entity) => {
						total[entity._id] = [
								$match: {
									entity: Mongo.toID(entity._id),
								$project: {
									_id: 0,
									from: 1,
									to: 1,
									type: 1,
									totalCO2e: 1,
									consumption: 1,
									totalCost: 1,
									totalWeight: 1,
									unit: 1,
						return total;
					}, {}),

“entities” is an array of objects that look like this:

{_id: "object id as a string", scale: number}

Basically, I create a pipeline for each id in the entities array inside the $facet.

For each entity id, we do a $match based on type which is a string and if the key entity matches the id

I then do a project to only return the data I need.

However, it would be great to stop a pipeline if there is not going to more than 10 documents returned for that entity id