Skip to content

Invalid variable reference in ArrayOperators.Filter [DATAMONGO-2320] #3177

@spring-projects-issues

Description

@spring-projects-issues

EGD opened DATAMONGO-2320 and commented

GroupOperation groupOperation = Aggregation.group(groupFields)
                    .count().as("count")
                    .push(Aggregation.CURRENT + "." + Fields.UNDERSCORE_ID).as("allIds");
MatchOperation matchDuplicates = Aggregation.match(where("count").gt(1));

ProjectionOperation firstElemeProjectionOperation = Aggregation.project("allIds")
                    .and(ArrayOperators.ArrayElemAt.arrayOf("allIds").elementAt(0)).as("firstElement");

ComparisonOperators.Ne duplicateCondition =
                    ComparisonOperators.Ne.valueOf("duplicate")
                            .notEqualTo("firstElement");

ArrayOperators.Filter excludeFirstIdInDuplicatesFilter =
                    ArrayOperators.Filter.filter("allIds").as("duplicate").by(duplicateCondition);
ProjectionOperation projectDuplicatesStage = Aggregation.project("allIds")
                    .andExclude(Fields.UNDERSCORE_ID)
                    .and(excludeFirstIdInDuplicatesFilter).as("dublicates");

Aggregation aggregation = Aggregation.newAggregation(
                    anyMatchOperation,
                    groupOperation,
                    matchDuplicates,
                    firstElemeProjectionOperation,
                    projectDuplicatesStage);
System.out.println("aggregation: " + aggregation.toString());

println return:

{ "aggregate" : "__collection__" , "pipeline" : [ { "$match" : { objectId: "any_object_id"} , { "$group" : { "_id" : { "field_1" : "$field_1" , "field_2" : "$field_2"} , "count" : { "$sum" : 1} , "allIds" : { "$push" : "$$CURRENT._id"}}} , { "$match" : { "count" : { "$gt" : 1}}} , { "$project" : { "allIds" : 1 , "firstElement" : { "$arrayElemAt" : [ "$allIds" , 0]}}} , { "$project" : { "allIds" : 1 , "_id" : 0 , "dublicates" : { "$filter" : { "input" : "$allIds" , "as" : "duplicate" , "cond" : { "$ne" : [ "$$duplicate" , "$$firstElement"]}}}}}]}

mongo expect this in cond $firstElement with one $, but actual value with two.

If run this aggreagtion, we get an error

com.mongodb.MongoCommandException: Command failed with error 17276: 'Use of undefined variable: firstElement'


Affects: 1.10.22 (Ingalls SR22)

Referenced from: pull request #776

Backported to: 2.1.10 (Lovelace SR10)

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions