Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-43666

Sort by "randVal" is not random

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • ALL
    • Hide
      MongoDB Enterprise > db.randval.insert([{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}])
      MongoDB Enterprise > db.randval.aggregate([{$sort: {rand: {$meta: "randVal"}}}])
      { "_id" : ObjectId("5d8d449cb417f08bc7302867"), "a" : 1 }
      { "_id" : ObjectId("5d8d449cb417f08bc7302868"), "a" : 2 }
      { "_id" : ObjectId("5d8d449cb417f08bc7302869"), "a" : 3 }
      { "_id" : ObjectId("5d8d449cb417f08bc730286a"), "a" : 4 }
      { "_id" : ObjectId("5d8d449cb417f08bc730286b"), "a" : 5 }
      
      Show
      MongoDB Enterprise > db.randval.insert([{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]) MongoDB Enterprise > db.randval.aggregate([{$sort: {rand: {$meta: "randVal"}}}]) { "_id" : ObjectId("5d8d449cb417f08bc7302867"), "a" : 1 } { "_id" : ObjectId("5d8d449cb417f08bc7302868"), "a" : 2 } { "_id" : ObjectId("5d8d449cb417f08bc7302869"), "a" : 3 } { "_id" : ObjectId("5d8d449cb417f08bc730286a"), "a" : 4 } { "_id" : ObjectId("5d8d449cb417f08bc730286b"), "a" : 5 }
    • Query 2019-11-04

      The SortKeyGenerator::extractKeyPart() function gets a "randVal" from the Document metadata (by way of Expression::evaluate()) when the key pattern has {$meta: ""randVal"}. However, nowhere in the code base ever sets a random value for this piece of metadata, so the return value is always eoo. The sort deals with this lack of randomness by returning the documents in their natural order.

      I discovered this problem when I attempted to add an invariant after this line to ensure that:

      invariant(!plainKey.missing());
      

      Currently, this invariant always fails, but when we figure out a way to ensure that documents have "randVal" metadata, we should be able to add that invariant in.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            justin.seyster@mongodb.com Justin Seyster
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: