gpt4 book ai didi

spring - 在 Spring Mongodb 中过滤搜索查询

转载 作者:可可西里 更新时间:2023-11-01 10:27:13 25 4
gpt4 key购买 nike

在提要集合中,“likeCount”和“commentCount”是两列。我想获取“likeCount”+“commentCount”大于 100 的所有文档。如何在 Spring Mongo DB 中编写搜索过滤器查询?

下面是我的示例提要收集数据。

{
"_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com