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
My answer is if using native query the variable passing to it is