0

Let say i have userInformation collection below structure

userInformation:
    - name : String
    - accounts[] : array

I need to covert below query into java springboot query.

db.userInformation.aggregate([{$match : {"name" : "madhu"}}, {$project: { "name": 1,
accountsCount: { $size:{ $filter: { input: "$accounts", as: "account", cond: {"$eq":["$$account.enabled", 0]}}}} }}]);

I have tried like below:

MatchOperation matchStage = Aggregation.match(new Criteria("name").is("madhu"); 
ProjectionOperation appProjectStage = Aggregation.project("name").
        and(new AggregationExpression() {
            @Override
            public Document toDocument(AggregationOperationContext context) {
                Document filterExpression = new Document();
                filterExpression.put("input", "$accounts");
                filterExpression.put("as", "account");
                filterExpression.put("cond",  new Document("$eq", Arrays.<Object> asList("$$account.enabled", 1)));
                return new Document("$filter", filterExpression);
            }
        }).as("filterdAccounts");

From there I could not able to convert that as count. Any help is appropriated.

Thanks

Madhuprathap
  • 209
  • 1
  • 3
  • 15

1 Answers1

0

You should try using ArrayOperators like this

Aggregation.project("name").and(ArrayOperators.Size.lengthOfArray(ArrayOperators.Filter.filter("$accounts").as("account").by(ComparisonOperators.Eq.valueOf("account.enabled").equalToValue(1)))).as("filterdAccounts"); 
Ashish Bakwad
  • 791
  • 4
  • 15