Skip to content

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

@aringeri

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));

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