spring mongo how to pass a variable in native mongo aggergation query

53 Views Asked by At

I need to do an aggregation query ,

 MatchOperation matchOperation = Aggregation.match(new Criteria("age").is(20));
    String lookup = "{$lookup: {from: 'race', localField: 'carId',foreignField: 'carId', as: 'result',},},";
    String unwind = " {\n" +
            "    $unwind: {\n" +
            "      path: \"$result\",\n" +
            "    },\n" +
            "  },";
    String addField =  "{ $addFields: { speed: '$result.raceSpeed', time: '$result.raceResulttime', progress: '$result.progress',},},";
    String matchField = " {\n" +
            "    $match:\n" +
            "      {\n" +
            "        time: {\n" +
            "          $gt: 0,\n" +
            "        },\n" +
            "      },\n" +
            "  },";
    String projectField = " {\n" +
            "    $project: {\n" +
            "      rank: 0,\n" +
            "      age: 0,\n" +
            "      phone: 0,\n" +
            "      role: 0,\n" +
            "      carImage: 0,\n" +
            "      creationDate: 0,\n" +
            "      gender: 0,\n" +
            "      _id: 0,\n" +
            "      result: 0,\n" +
            "    },\n" +
            "  },";
    SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC,"time");
    Aggregation aggregation = Aggregation.newAggregation(
            matchOperation,
            new CustomAggregationOperation(lookup),
            new CustomAggregationOperation(unwind),
            new CustomAggregationOperation(addField),
            new CustomAggregationOperation(matchField),
            new CustomAggregationOperation(projectField),
            sortOperation
    );
    List<AggResult> outputs= mongoTemplate.aggregate(aggregation, "user",
            AggResult.class).getMappedResults();

i need to work some some variable in match field , like

 String matchField = " {\n" +
            "    $match:\n" +
            "      {\n" +
            "        time: {\n" +
            "          $gt: timevariable,\n" + <============== wanna pass timevariable here
            "        },\n" +
            "      },\n" +
            "  },";

I have try the solution of escape in here enter link description here but without success, would appreciate if someone can offer help, thanks

2

There are 2 best solutions below

0
On

My answer is if using native query the variable passing to it is

  String iamvairable = "177";
    System.out.println(iamvairable);
    String projectField = " {\n" +
            "    $project: {\n" +
            "      rank: 0,\n" +
            "      age: "+iamvairable+",\n" +
            "      phone: 0,\n" +
            "      role: 0,\n" +
            "      carImage: 0,\n" +
            "      creationDate: 0,\n" +
            "      gender: 0,\n" +
            "      _id: 0,\n" +
            "      result: 0,\n" +
            "    },\n" +
            "  },";
    System.out.println("test string - " + projectField);
0
On

Consider using Bson to create a pipeline and execute it with MongoTemplate. It is much more readable and debugable. Also, it allows you to do everything you would do in a native Mongo query.

List<Bson> pipeline = new ArrayList<>();
        pipeline.add(new Document(MongoOperations.MATCH, new Document("_id", bankId)));
        pipeline.add(new Document(MongoOperations.UNWIND, "$versions"));
        pipeline.add(new Document(MongoOperations.MATCH, new Document("versions.active", Boolean.TRUE)
                .append("versions.date", new Document("$gte", startDate).append("$lte", endDate))));
Document document = mongoTemplate.getCollection(COLLECTION)
                .aggregate(pipeline, Document.class).first();