简介
尝试计算字段为"seed"的用户等于"false"的次数
(邮件(收集示例
{
to : "jimmy",
from : "johny",
message : "Hey",
seen: false
},
{
to : "jimmy",
from : "Maggy",
message : "Hello",
seen: true
},
{
to : "jimmy",
from : "Sam",
message : "How are you?",
seen: false
},
{
to : "jimmy",
from : "johny",
message : "It's me again",
seen: false
}
Java方法w/Mongo聚合
//WHERE 'to' EQUALS 'jimmmy'
//COUNT 'seen' EQUALS 'false'
我尝试过的
public int getTotalUnseen(String toUser){
AggregateIterable<Document> iterable = db.getCollection("mail").aggregate(asList(
new Document("$match", new Document("to", "jimmy")),
new Document("$group", new Document("_id", "$seen").append("count", new Document("$sum", 1)))));
}
期望结果
int x = getTotalUnseenCount("jimmy");
System.out.println("Result : "+ x);
//Result : 3
您可以尝试以下操作:
Query query = new Query();
query.addCriteria(Criteria.where("to").is("jimmy").and("seen").is(true));
mongoTemplate.count(query, "mail");