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

Use of $$ROOT in $expr in $match fails on servers prior to 6.0

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Optimization
    • ALL
    • Hide

      Create test data:

      db.C.insertMany([{ _id : 1, Name : "John Doe" }, { _id : 2, Name : "Jane Doe" }])
      

       Execute command:

      db.C.aggregate([{ $match : { $expr : { $eq : ['$$ROOT', { _id : 2, Name : 'Jane Doe' }] } } }])
      

      Expected result:

      > db.C.aggregate([{ $match : { $expr : { $eq : ['$$ROOT', { _id : 2, Name : 'Jane Doe' }] } } }])
      [ { _id: 2, Name: 'Jane Doe' } ]
      

      Result in 5.0:

      > db.C.aggregate([{ $match : { $expr : { $eq : ['$$ROOT', { _id : 2, Name : 'Jane Doe' }] } } }])
      MongoServerError: Failed to optimize pipeline :: caused by :: Failed to optimize expression :: caused by :: FieldPath::tail() called on single element path
      
      Show
      Create test data: db.C.insertMany([{ _id : 1, Name : "John Doe" }, { _id : 2, Name : "Jane Doe" }])  Execute command: db.C.aggregate([{ $match : { $expr : { $eq : [ '$$ROOT' , { _id : 2, Name : 'Jane Doe' }] } } }]) Expected result: > db.C.aggregate([{ $match : { $expr : { $eq : [ '$$ROOT' , { _id : 2, Name : 'Jane Doe' }] } } }]) [ { _id: 2, Name: 'Jane Doe' } ] Result in 5.0: > db.C.aggregate([{ $match : { $expr : { $eq : [ '$$ROOT' , { _id : 2, Name : 'Jane Doe' }] } } }]) MongoServerError: Failed to optimize pipeline :: caused by :: Failed to optimize expression :: caused by :: FieldPath::tail() called on single element path

      The following aggregate command works on 6.0 but not on earlier versions:

      db.C.aggregate([{ $match : { $expr : { $eq : ['$$ROOT', { _id : 2, Name : 'Jane Doe' }] } } }])
      

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: