通过将两个集合与spring数据mongodb数据中的条件合并来获取计数

时间:2018-01-18 16:50:07

标签: java mongodb spring-data aggregation-framework spring-data-mongodb

我有两个名为广告&包含的内容。 我需要一个包含contains的包含的状态是" ACTIVE" &安培;该广告当前正在进行的月份中的lastModifiedDate。

此外,我还有广告的条件,即adState是" ACTIVE"。

advertisement document
{

    "_id" : ObjectId("1231321321321"),
    "adNumber":1
    "adState" : "ACTIVE"
    "company" : "companyABC",
    "address" : "123 new road",
    "agentName": "Agent_2"
    "lastpublish" : ISODate("2018-01-18T20:20:02.262Z"),
    "createdDate" : ISODate("2018-01-10T16:03:06.694Z"),
    "lastModifiedDate" : ISODate("2018-01-18T20:20:02.262Z"),
    "createdBy" : "yasa",
    "lastModifiedBy" : "System",   
}

    contains documents

{   

    "_id" : ObjectId("465465456456"),
    "adNumber":1
    "containId": 10
    "containState" : "ACTIVE",
    "title" : "Increase sales by 10%",
    "detials" : "buy this solution to incresce ur sale by 10%",
    "agentName": "Agent_2"
    "createdDate" : ISODate("2018-01-11T15:03:06.694Z"),
    "lastModifiedDate" : ISODate("2018-01-11T15:03:06.694Z"),
    "createdBy" : "yasa",
    "lastModifiedBy" : "System",   
}

{    

    "_id" : ObjectId("56565656555"),
    "adNumber":1
    "containId": 11
    "containState" : "IN-ACTIVE",
    "title" : "Land for sale",
    "detials" : "Land near water park for sale",
    "agentName": "Agent_2"
    "createdDate" : ISODate("2018-01-11T15:30:01.694Z"),
    "lastModifiedDate" : ISODate("2018-01-12T15:03:06.694Z"),
    "createdBy" : "yasa",
    "lastModifiedBy" : "System",   
}

{


    "_id" : ObjectId("56887423587"),
    "adNumber":1
    "containId": 12
    "containState" : "ACTIVE",
    "title" : "car for sale",
    "detials" : "BMW for sale",
    "agentName": "Agent_2"
    "createdDate" : ISODate("2018-01-11T15:45:01.690Z"),
    "lastModifiedDate" : ISODate("2018-01-11T15:45:01.690Z"),
    "createdBy" : "yasa",
    "lastModifiedBy" : "System",   
}

给出代码示例以在spring-data,java中的mongodb上合并它。

同时建议最佳方式获取结果聚合或地图缩小

汇总查询

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 } }}])

2 个答案:

答案 0 :(得分:2)

您可以在spring mongo 2.x和Mongo 3.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");

答案 1 :(得分:1)

以下是mongo聚合管道,您需要获取计数

步骤

  1. $ match - 匹配广告ID
  2. $ lookup - 使用contains
  3. 加入
  4. $ unwind - 将数组拆分为个人doc
  5. $ addFields - 用于检查正在进行的月份的字段
  6. $ match - 过滤非匹配文档
  7. $ group - 算上比赛
  8. 您可以将其翻译为JavaSpringMongoRepositories

    管道

    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 } } }
        ]
    )