spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

MongoCommandException while using of $all in aggregation #3762

Open antdementyev opened 3 years ago

antdementyev commented 3 years ago

Structure of documents:

{
  id: ObjectId("5ef1cd704b35c6d6698f2050")
  date: 2021-12-15T11:03:00.000+00:00
  priorityList: Array
    0: "any-String-id-here"
    1: ...
}

Using of $all in Aggregation:

TypedAggregation<MyDocument> aggregation = newAggregation(MyDocument.class,
  project("id")
    .and("isInList").applyCondition(ConditionalOperators
        .when(Criteria.where("priorityList").all("here-my-string-to-check"))
        .then(1)
        .otherwise(0)),
  sort(Sort.by(
    Sort.Order.desc("isInList"),
    Sort.Order.desc("date"))));
mongoTemplate.aggregate(aggregation, "my-collection-name", String.class);

produces

com.mongodb.MongoCommandException: 
Command failed with error 168 (InvalidPipelineOperator): 'Invalid $project ::
caused by :: Unrecognized expression '$all''
christophstrobl commented 3 years ago

Do you have a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem? The pipeline stage looks ok to me.

"$project": {
  "id": 1,
  "isInList": {
    "$cond": {
      "if": {
        "$all": [
          "$priorityList",
          [
            "here-my-string-to-check"
          ]
        ]
      },
      "then": 1,
      "else": 0
    }
  }
}

Have you tried to run the aggregation on the shell? If so, does it work and what does it look like?

antdementyev commented 3 years ago

Sorry I can't prepare a sample for time reasons. But running of aggregation on the shell results the same error: MongoServerError: Invalid $project :: caused by :: Unrecognized expression '$all'

$all is not listed in the mongo doc https://docs.mongodb.com/manual/reference/operator/aggregation/ Current doc to spring doesn't support $all too: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation.supported-aggregation-operations#mongo.aggregation.supported-aggregation-operations

So it seems to be not a bug in the implementation but just the existing of an unsupported feature in the spring API.