如何在springboot条件中编写mongo查询

lmvvr0a8  于 2023-01-17  发布在  Spring
关注(0)|答案(1)|浏览(144)

关于如何在spring Boot criteria中编写此查询的一些帮助。

db.collection.aggregate([
    {"$group" : { "_id": "$field1", "count": { "$sum": 1 } } },
    {"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } }, 
    {"$project": {"name" : "$_id", "_id" : 0} }
])
x8diyxa7

x8diyxa71#

您可以尝试以下代码:

GroupOperation group = Aggregation.group("field1").count().as("count");
MatchOperation match = Aggregation.match(Criteria.where("_id").ne(null).andOperator(Criteria.where("count").gt(1)));
ProjectionOperation project = Aggregation.project().andExpression("_id").as("name").andExclude("_id");

Aggregation aggregation = Aggregation.newAggregation(group, match, project);

mongoTemplate.aggregate(aggregation, "collection", YourClass.class);

文件:

和一个Baeldung example

相关问题