在 feed 集合中,“likeCount”和“commentCount”是两列。我想获取“likeCount”+“commentCount”大于100的所有文档。如何在Spring Mongo DB中编写搜索过滤器查询?
下面是我的示例 Feed 收集数据。
{
"_id" : ObjectId("55deb33dcb9be727e8356289"),
"channelName" : "Facebook",
"likeCount" : 2,
"commentCount" : 10,
}
为了比较单个字段,我们可以编写搜索查询,例如:
BasicDBObject searchFilter = new BasicDBObject();
searchFilter.append("likeCount", new BasicDBObject("$gte",100));
DBCursor feedCursor = mongoTemplate.getCollection("feed").find(searchFilter);
最佳答案
试试这个
db.collection.aggregate([{$project:{total:{'$add':["$likeCount","$commentCount"]}}},{$match:{total:{$gt:100 }}}])
关于spring - Spring Mongodb 中的过滤器搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775671/