Visitar URL original
Type errors when applying aggregation array operators on result of $split · Issue #214 · bwaldvogel/mongo-java-server · GitHub
Skip to content

Type errors when applying aggregation array operators on result of $split #214

Closed

Description

@aringeri

Hi, I'm seeing an error when attempting to apply an array operator on the result of a string $split.

If I run an aggregation like this:

db.MY_DOC.aggregate([
   { 
      $addFields: { 
         pathSegments: { 
            $split: ["$path", "/"]
         },
         firstSegment: {
            $arrayElemAt: ["$pathSegments", 0]
         }
      }
   }
])

I see an error like this:

MongoServerError: PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;

When I run from a java environment, I get this error

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 28689 (Location28689): 'PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;' on server localhost:12345. The full response is {"$err": "PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;", "errmsg": "PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;", "code": 28689, "codeName": "Location28689", "ok": 0}; nested exception is com.mongodb.MongoCommandException: Command failed with error 28689 (Location28689): 'PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;' on server localhost:12345. The full response is {"$err": "PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;", "errmsg": "PlanExecutor error during aggregation :: caused by :: $arrayElemAt's first argument must be an array, but is [Ljava.lang.String;", "code": 28689, "codeName": "Location28689", "ok": 0}
	at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133)
	at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2882)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:564)
	at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2130)
	at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2099)
	at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1989)

Looking at the mongo-java-server code. It looks like $arrayElemAt is expecting a List type, but the $split return type is a String[]:

return ((String) string).split(Pattern.quote((String) delimiter));

Activity

  1. bwaldvogel commented on May 31, 2023

    @bwaldvogel
    Owner

    Thanks for the bug report. This should be fixed via 5e478bc.

    Please note that your example aggregation doesn’t work as you probably expected with a real MongoDB either. At least in my tests with MongoDB 5.0.18 and 6.0.6. Can you confirm?
    TBH, this behavior surprised me. I would be interested if the behavior is documented?

  2. bwaldvogel commented on Jun 25, 2023

    @bwaldvogel
    Owner

    The fix is available via v1.44.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions