Java 通过将两个集合与spring data mongodb data中的条件合并来获取计数
我有两个集合叫做广告和包含。 我需要该广告当前进行中的月份中containState为“活动”的包含数&lastModifiedDate 我也有一个广告条件,广告是“积极的” 给出在spring数据、java中的mongodb上合并的代码示例 还建议获得结果聚合或映射减少的最佳方法 聚合查询Java 通过将两个集合与spring data mongodb data中的条件合并来获取计数,java,mongodb,spring-data,aggregation-framework,spring-data-mongodb,Java,Mongodb,Spring Data,Aggregation Framework,Spring Data Mongodb,我有两个集合叫做广告和包含。 我需要该广告当前进行中的月份中containState为“活动”的包含数&lastModifiedDate 我也有一个广告条件,广告是“积极的” 给出在spring数据、java中的mongodb上合并的代码示例 还建议获得结果聚合或映射减少的最佳方法 聚合查询 db.advertisement.aggregate([ {$match:{"adState": "ACTIVE", "agentName": "Agent_2"}}, {$lookup: {
db.advertisement.aggregate([
{$match:{"adState": "ACTIVE", "agentName": "Agent_2"}},
{$lookup:
{
from: "contains",
localField: "adNumber",
foreignField: "adNumber",
as: "result_ad"
}},
{$project:{result_ad:1}},
{$match:{"result_ad": {'$ne': []}}},
{$unwind:"$result_ad"},
{$match:{"result_ad.containState" : "ACTIVE"}},
{$group: { _id: null, count: { $sum: 1 } }}])
下面是mongo聚合管道,您需要获得计数 台阶
Java
、Spring
和MongoRepositories
管道
db.advertisement.aggregate(
[
{ $match : { "adNumber" : 1 } },
{ $lookup : {
from : "contains",
localField : "adNumber",
foreignField : "adNumber",
as : "cons"
}
},
{ $unwind : "$cons" },
{ $addFields : {
monthMatch : { $eq : [ { $month : new Date() }, { $month : "$cons.lastModifiedDate" } ] }
}
},
{ $match : { "cons.containState" : "ACTIVE", monthMatch : true} },
{ $group : { _id : null, count : { $sum : 1 } } }
]
)
您可以在SpringMongo2.x和Mongo3.4版本中尝试下面的spring聚合查询
Instant startofMonth = LocalDate.now()
.with( TemporalAdjusters.firstDayOfMonth() ).atStartOfDay().toInstant(ZoneOffset.UTC);
Instant endofMonth = LocalDate.now()
.with( TemporalAdjusters.lastDayOfMonth() ).atTime(LocalTime.MAX).toInstant(ZoneOffset.UTC);
MatchOperation matchOperation = Aggregation.match(Criteria.where("adState").is("ACTIVE").and("agentName").is("Agent_2"));
LookupOperation lookupOperation = LookupOperation.newLookup().
from("contains").
localField("adNumber").
foreignField("adNumber").
as("result_ad");
UnwindOperation unwindOperation = Aggregation.unwind("result_ad");
MatchOperation matchOperation2 = Aggregation.match(Criteria.where("result_ad.containState").is("ACTIVE").and("result_ad.lastModifiedDate").gte(startofMonth).lte(endofMonth));
CountOperation countOperation = Aggregation.count().as("count");
Aggregation aggregation = Aggregation.newAggregation(matchOperation, lookupOperation, unwindOperation, matchOperation2, countOperation);
Integer results = mongoOperations.aggregate(aggregation, colname, Document.class).getUniqueMappedResult().getInteger("count");
你试过什么?请告诉我们您拥有的代码,有人会从那里帮助您。这两个集合是否相互关联?您期望的json输出是什么?添加所有用于获得精确答案的POJO。@Veeram我已经为此编写了一个mongodb查询,我不知道如何使用spring数据,我还需要过滤当前月份的数据。我已经在questionOk上添加了mongodb聚合查询,我正在尝试使用它,如果您提供与spring数据匹配的指南,并提供聚合的示例java文档,我想这会很好,但我收到一个编解码器异常。但是我已经向编解码器提供程序pojoCodecProvider=pojoCodecProvider.builder()注册了mongoclient。注册(“com.myapp.domain”).build();CodecRegistry pojoCodecRegistry=fromRegistries(MongoClient.getDefaultCodecRegistry(),fromProviders(pojoCodecProvider));MongoClientURI MongoClientURI=new MongoClientURI(environment.getProperty(“mongodb.uri”)、mongoclientations.builder().codecRegistry(pojoCodecRegistry));MongoClient MongoClient=新的MongoClient(MongoClient);返回mongoClient;我已经为advision创建了域类,Agent,Contains,目前也为agentName添加了DBref,所以我做了如下更改:MatchOperation MatchOperation=Aggregation.match(Criteria.where(“adState”).is(“ACTIVE”);和(“agentName”).is(Agent))//代理是反对的。你能用所有的细节更新帖子吗?还是最好创建一个新问题?很难猜出问题出在哪里。重现的步骤将有助于调试问题。
Instant startofMonth = LocalDate.now()
.with( TemporalAdjusters.firstDayOfMonth() ).atStartOfDay().toInstant(ZoneOffset.UTC);
Instant endofMonth = LocalDate.now()
.with( TemporalAdjusters.lastDayOfMonth() ).atTime(LocalTime.MAX).toInstant(ZoneOffset.UTC);
MatchOperation matchOperation = Aggregation.match(Criteria.where("adState").is("ACTIVE").and("agentName").is("Agent_2"));
LookupOperation lookupOperation = LookupOperation.newLookup().
from("contains").
localField("adNumber").
foreignField("adNumber").
as("result_ad");
UnwindOperation unwindOperation = Aggregation.unwind("result_ad");
MatchOperation matchOperation2 = Aggregation.match(Criteria.where("result_ad.containState").is("ACTIVE").and("result_ad.lastModifiedDate").gte(startofMonth).lte(endofMonth));
CountOperation countOperation = Aggregation.count().as("count");
Aggregation aggregation = Aggregation.newAggregation(matchOperation, lookupOperation, unwindOperation, matchOperation2, countOperation);
Integer results = mongoOperations.aggregate(aggregation, colname, Document.class).getUniqueMappedResult().getInteger("count");