• Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization
    • ALL
    • Hide
      db.a.drop()
      db.a.insertMany([{}, {}])
      db.a.aggregate([{$lookup: {from: "a", pipeline: [{$facet: {f: [{$set: {r: {$rand: {}}}}]}}], as: "res"}}]) 

      This produces something like:

      [
        {
          _id: ObjectId('6aaabda0460aabd8d9d1a7bd'),
          res: [
            {
              f: [
                { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.9609455310720872 },
                { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.6046304842407205 }
              ]
            }
          ]
        },
        {
          _id: ObjectId('6aaabda0460aabd8d9d1a7be'),
          res: [
            {
              f: [
                { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.9609455310720872 },
                { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.6046304842407205 }
              ]
            }
          ]
        }
      ]

      Note that the chosen random numbers are identical for both outer documents.

      For comparison, here is the same query without $facet and the output:

      db.a.aggregate([{$lookup: {from: "a", pipeline: [{$set: {r: {$rand: {}}}}], as: "res"}}])
      [
        {
          _id: ObjectId('6aaabda0460aabd8d9d1a7bd'),
          res: [
            { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.48796429148767273 },
            { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.37433400902710473 }
          ]
        },
        {
          _id: ObjectId('6aaabda0460aabd8d9d1a7be'),
          res: [
            { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.591967270406342 },
            { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.5688078786300821 }
          ]
        }
      ]
      Show
      db.a.drop() db.a.insertMany([{}, {}]) db.a.aggregate([{$lookup: {from: "a" , pipeline: [{$facet: {f: [{$set: {r: {$rand: {}}}}]}}], as: "res" }}]) This produces something like: [   {     _id: ObjectId('6aaabda0460aabd8d9d1a7bd'),     res: [       {         f: [           { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.9609455310720872 },           { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.6046304842407205 }         ]       }     ]   },   {     _id: ObjectId('6aaabda0460aabd8d9d1a7be'),     res: [       {         f: [           { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.9609455310720872 },           { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.6046304842407205 }         ]       }     ]   } ] Note that the chosen random numbers are identical for both outer documents. For comparison, here is the same query without $facet and the output: db.a.aggregate([{$lookup: {from: "a" , pipeline: [{$set: {r: {$rand: {}}}}], as: "res" }}]) [   {     _id: ObjectId('6aaabda0460aabd8d9d1a7bd'),     res: [       { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.48796429148767273 },       { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.37433400902710473 }     ]   },   {     _id: ObjectId('6aaabda0460aabd8d9d1a7be'),     res: [       { _id: ObjectId('6aaabda0460aabd8d9d1a7bd'), r: 0.591967270406342 },       { _id: ObjectId('6aaabda0460aabd8d9d1a7be'), r: 0.5688078786300821 }     ]   } ]
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Per SERVER-49024, $lookup should not cache subpipelines with $rand, but it misses $rand-s inside $facet-s.

            Assignee:
            Unassigned
            Reporter:
            Kartal Kaan Bozdogan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: