0

Inside my existing pipeline, I wanna get a sample of 10 documents from another collection (another collection different than the one that the pipeline is targeted at).

I could do this with $lookup, but it would perform this search in other collection one time for every individual record in the main pipeline, and this could greatly decrease the performance.

The final result would be something similar to the one with lookup, but without the server load of performing one lookup for every single record. It would be kinda like a general lookup ran only once for the query.

Is it possible?

Here's the code I have so far, but it performs multiple lookups, one for each record in the main collection, which gives me poor performance that tends to get worse as the amount of documents in the original collection grows

{
  $facet: {
    array1: [
      {
        $match: {
          _id: req.params.id,
        },
      },
    ],
    array2: [
      {
        $match: {
              $expr: {
                $ne: ["$_id", req.params.id],
              },
        },
      },
    ],
    array3: [
      {
        $lookup: {
          from: "external_collection",
          pipeline: [{ $sample: { size: 10 } }],
          as: "random_suggestions",
        },
      },
      {
        $project: {
          _id: 0,
          random_suggestions: 1,
        },
      },
    ],
  }
}
Pedro Rabbi
  • 193
  • 1
  • 12
  • Put your `$sample` in a [`$unionWith`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith/)? – ray Mar 06 '23 at 19:52
  • db.collection.aggregate([ { $facet: { collection1Results: [ // steps of search for collection 1 ], collection2Results: [ // steps of search for collection 2 ] } }, { $project: { results: { $unionWith: ["$collection1Results", "$collection2Results"] } } } ]) – Pedro Rabbi Mar 06 '23 at 19:59
  • I dont think it solves my problem – Pedro Rabbi Mar 06 '23 at 19:59
  • 2
    Can you share your sample documents and expected output? – ray Mar 06 '23 at 20:00

0 Answers0