java.lang.IllegalArgumentException: Invalid reference '_id.eventID'!

Spring JIRA | Christine Chan | 3 years ago
  1. 0

    The following query in mongo is as such: {code} db.auditErrorEvents.aggregate( {"$group":{_id:{ "eventID" : "$event.eventID" , "appID" : "$event.appID" , "eventUID" : "$event.eventUID" , "osPlatform" : "$event.osPlatform" , "deviceType" : "$event.deviceType"}}}, {"$group":{_id:{ "eventID" : "$_id.eventID" , "appID" : "$_id.appID" , "osPlatform" : "$_id.osPlatform" , "deviceType" : "$_id.deviceType"}, "count" : { "$sum" : 1}}} ); {code} Basically this is a distinct count of a field eventUID with grouping eventID, appID, osPlatform, deviceType. The {{MongoTemplate}} does not allow me to refer to the first grouping. {code} template.aggregate(newAggregation( group(from(field("eventID", "event.eventID"), field("appID", "event.appID"), field("eventUID", "event.eventUID"), field("osPlatform", "event.osPlatform"), field("deviceType", "event.deviceType"))), group(from(field("eventID", "_id.eventID"), field("appID", "_id.appID"), field("osPlatform", "_id.osPlatform"), field("deviceType"))).count().as("count")), collectionName(), GroupedCount.class); {code} {noformat} java.lang.IllegalArgumentException: Invalid reference '_id.eventID'! at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:78) at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:62) at org.springframework.data.mongodb.core.aggregation.GroupOperation.toDBObject(GroupOperation.java:300) {noformat}

    Spring JIRA | 3 years ago | Christine Chan
    java.lang.IllegalArgumentException: Invalid reference '_id.eventID'!
  2. 0

    The following query in mongo is as such: {code} db.auditErrorEvents.aggregate( {"$group":{_id:{ "eventID" : "$event.eventID" , "appID" : "$event.appID" , "eventUID" : "$event.eventUID" , "osPlatform" : "$event.osPlatform" , "deviceType" : "$event.deviceType"}}}, {"$group":{_id:{ "eventID" : "$_id.eventID" , "appID" : "$_id.appID" , "osPlatform" : "$_id.osPlatform" , "deviceType" : "$_id.deviceType"}, "count" : { "$sum" : 1}}} ); {code} Basically this is a distinct count of a field eventUID with grouping eventID, appID, osPlatform, deviceType. The {{MongoTemplate}} does not allow me to refer to the first grouping. {code} template.aggregate(newAggregation( group(from(field("eventID", "event.eventID"), field("appID", "event.appID"), field("eventUID", "event.eventUID"), field("osPlatform", "event.osPlatform"), field("deviceType", "event.deviceType"))), group(from(field("eventID", "_id.eventID"), field("appID", "_id.appID"), field("osPlatform", "_id.osPlatform"), field("deviceType"))).count().as("count")), collectionName(), GroupedCount.class); {code} {noformat} java.lang.IllegalArgumentException: Invalid reference '_id.eventID'! at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:78) at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:62) at org.springframework.data.mongodb.core.aggregation.GroupOperation.toDBObject(GroupOperation.java:300) {noformat}

    Spring JIRA | 3 years ago | Christine Chan
    java.lang.IllegalArgumentException: Invalid reference '_id.eventID'!
  3. 0

    Mongo Shell working command. {code} db.person.aggregate( { $unwind: "$descriptors.carDescriptor.entries" }, {$project: {make: "$descriptors.carDescriptor.entries.make", model: "$descriptors.carDescriptor.entries.model", firstName:"$firstName", lastName: "$lastName"}}, {$group: {_id: "$make"}} ) {code} Equivalent Spring Data representation {code} Aggregation agg = Aggregation.newAggregation( unwind("descriptors.carDescriptor.entries"), project() .and("descriptors.carDescriptor.entries.make").as("make") .and("descriptors.carDescriptor.entries.model").as("model") .and("firstName").as("firstName") .and("lastName").as("lastName"), group("make") ); {code} While the Mongo Shell query work fine, Spring MongoTemplate form does not and returns the following error {noformat} java.lang.IllegalArgumentException: Invalid reference 'make'! at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:92) at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:62) at org.springframework.data.mongodb.core.aggregation.GroupOperation.toDBObject(GroupOperation.java:292) at org.springframework.data.mongodb.core.aggregation.Aggregation.toDbObject(Aggregation.java:247) at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1323) at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1306) at com.example.samples.MongoTemplateTest.testAggregation(MongoTemplateTest.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) {noformat} Pojo classes {code} // getter, setter omitted @Document public class Person { @Id private String id; private String firstName; private String lastName; private Descriptors descriptors; } public class Descriptors { private CarDescriptor carDescriptor; } public class CarDescriptor { private List<Entry> entries; class Entry { private String make; private String model; private int year; } } {code} To confirm if "make" is being returned remove the group("make") operator from the fluent api and the output shows that the make is indeed being returned. Any pointers / work arounds ? Thank you, Adil Fulara

    Spring JIRA | 3 years ago | Adil Fulara
    java.lang.IllegalArgumentException: Invalid reference 'make'!
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0
  6. 0

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalArgumentException

      Invalid reference '_id.eventID'!

      at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference()
    2. Spring Data MongoDB - Core
      GroupOperation.toDBObject
      1. org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:78)
      2. org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:62)
      3. org.springframework.data.mongodb.core.aggregation.GroupOperation.toDBObject(GroupOperation.java:300)
      3 frames