Spring-data-mongo aggregate filters that the two fields are equal.

  mongodb, question

When using spring-data-mongo for statistics, it is necessary to compare whether the two fields are equal, for example, l==qzt
Like a query in find


This code can be found out
However, when using spring-data-mongo Aggregation, the following error is reported

"errmsg" : "$where is not allowed inside of a $match aggregation expression" , "code" : 16395

This is how I use it:

Criteria c = Criteria.where("$where").is("this.qzt==this.l");
 Aggregation agg =Aggregation.newAggregation(
 Aggregation.sort(Sort.Direction.DESC, "z")
 List<Events2> results = events2Data.agreagate(agg);

May I ask if there is anything wrong with you, or is there any way to compare whether the two fields are equal? Thank you ~

I am sorry that I have only added an answer now. I have already found the answer to this question. It is to manually splice query statements and carry out aggregate query by using the Aggregate method provided by mongo-driver. The implementation code is as follows:

DBObject group = (DBObject) JSON.parse("{'$group': {'_id': {'qzc':'$qzc','qzt':'$qzt'},'z': {'$sum':1}}}");
 DBObject project = (DBObject) JSON.parse("{'$project': {'z':1,'difference': { '$eq': [ '$l', '$qzt' ]}}}");
 DBObject match = (DBObject) JSON.parse("{'$match': {'difference':true,'l':{'$ne':0}}}");
 DBObject sort = (DBObject) JSON.parse("{'$sort': {'z':-1}}");
 AggregationOutput output = mongoTemplate.getCollection("events2").aggregate(group, project, match, sort);
 Iterator<DBObject> it = output.results().iterator();
 List<String> result = new ArrayList<String>();
 EventType eventType = new EventType();
 while (it.hasNext()) {
 DBObject db = it.next();
 Map<String, String> _id = (Map<String, String>) db.get("_id");
 String qzc = _id.get("qzc");
 String qzt = _id.get("qzt");
 Integer z = (Integer) db.get("z");
 String type = eventType.get(qzt);
 result.add(qzc+" "+type+" "+z);