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

$expr does not seem to work with fields in array

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 3.6.0-rc2
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      use jira;
       
      db.expr.insert({
      	"_id":"test",
      	"max"{
      		"date": ISODate('2017-11-08T00:00:00Z')
      	},
      	"arr":[
      		{'id':1, "date": ISODate('2017-11-01T00:00:00Z')},
      		{'id':2, "date": ISODate('2017-11-03T00:00:00Z')},
      		{'id':3, "date": ISODate('2017-11-05T00:00:00Z')},
      		{'id':4, "date": ISODate('2017-11-07T00:00:00Z')},
      		{'id':5, "date": ISODate('2017-11-08T00:00:00Z')},
      	],
      	'maxdate': ISODate('2017-11-08T00:00:00Z')
      });
       
      // Works
      db.expr.aggregate([	{
      	  "$match": {
      	    "$expr":
      	        {"$eq": ["$maxdate", "$max.date"]
      	    }
      	    }
      	  }
      ]);
       
      // Works
      db.expr.aggregate([
      	{
      		'$project':{"md":{"$max":"$arr.date"}, "maxdate":1},
      	},
      	{
      	  "$match": {
      	    "$expr":
      	        {"$eq": ["$maxdate", "$md"]
      	    }
      	    }
      	  }
      ]);
       
      // Does not work
      db.expr.aggregate([	{
      	  "$match": {
      	    "$expr":
      	        {"$eq": ["$maxdate", "$arr.date"]
      	    }
      	    }
      	  }
      ]);
       
      // Does not work either
      db.expr.aggregate([
      	{
      		'$addFields':{"md":{"$max":"$arr.date"}, "maxdate":1},
      	},
      	{
      	  "$match": {
      	    "$expr":
      	        {"$eq": ["$maxdate", "$md"]
      	    }
      	    }
      	  }
      ]);
      
      

      Show
      use jira;   db.expr.insert({ "_id" : "test" , "max" { "date" : ISODate( '2017-11-08T00:00:00Z' ) }, "arr" :[ { 'id' :1, "date" : ISODate( '2017-11-01T00:00:00Z' )}, { 'id' :2, "date" : ISODate( '2017-11-03T00:00:00Z' )}, { 'id' :3, "date" : ISODate( '2017-11-05T00:00:00Z' )}, { 'id' :4, "date" : ISODate( '2017-11-07T00:00:00Z' )}, { 'id' :5, "date" : ISODate( '2017-11-08T00:00:00Z' )}, ], 'maxdate' : ISODate( '2017-11-08T00:00:00Z' ) });   // Works db.expr.aggregate([ { "$match" : { "$expr" : { "$eq" : [ "$maxdate" , "$max.date" ] } } } ]);   // Works db.expr.aggregate([ { '$project' :{ "md" :{ "$max" : "$arr.date" }, "maxdate" :1}, }, { "$match" : { "$expr" : { "$eq" : [ "$maxdate" , "$md" ] } } } ]);   // Does not work db.expr.aggregate([ { "$match" : { "$expr" : { "$eq" : [ "$maxdate" , "$arr.date" ] } } } ]);   // Does not work either db.expr.aggregate([ { '$addFields' :{ "md" :{ "$max" : "$arr.date" }, "maxdate" :1}, }, { "$match" : { "$expr" : { "$eq" : [ "$maxdate" , "$md" ] } } } ]);
    • Case:

      Description

      Trying to match a field with another one in an array does not seem to be working.

      Also, using $expr to compare a projected field work, but not a new field created with $addFields

      Step to reproduce demonstrate the issue.

      I'm filling this as a major bug because this looks like important issues in the $expr mechanism to me.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: