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

$expr does not seem to work with fields in array

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.0-rc2
    • Component/s: Querying
    • None
    • ALL
    • 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" ] } } } ]);

      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.

            Assignee:
            james.wahlin@mongodb.com James Wahlin
            Reporter:
            hmducoulombier@marketing1by1.com Henri-Maxime Ducoulombier
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: