title: Get counts by merge two collections with conditions in spring data mongodb data
body: <p>I have two collections called advertisement &amp; contains.
  I need a count of contains that containState is "ACTIVE" &amp; lastModifiedDate in current ongoing month for that advertisement.</p>
  
  <p>Also I have a conditions on advertisement that adState is "ACTIVE".</p>
  
  <pre><code>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",   
  }
  </code></pre>
  
  <p>Give code example to merge this on spring-data, mongodb in java.</p>
  
  <p>Also suggest best way get result Aggregation or map reduce</p>
  
  <p>Aggregate query</p>
  
  <pre><code>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 } }}])
  </code></pre>