2

Select leadId count on two collection in Mongo DB

Collection 1 : leads

{
 leadId:"abc123",
 status:"OPENED",
 stage:"start",
 crossSell:
    {
      cc:
         {
            consent:true,
            shown:[{first:true}]
         }
    }
 }

Collection 2 : pdata

{
 activeLeadId:"abc123",
 status:"OPENED",
 details:
    [
        {
            rating:10
        },
        {
            rating:9
        }
    ]
}

Question : Find leadId count from leads collection join with pdata collection based on below conditions

  1. leads.leadId = pdata.activeleadId and
  2. leads.status = "OPENED" and
  3. leads.crossSell.cc.consent = true and
  4. leads.crossSell.cc.shown[0].first = true and
  5. pdata.details.rating >= 5
4
  • what is the expected output in json? you want to unwind the arrays first and then do the lookup? or you want lookup with the arrays as they are? for example rating>=5 what it means? array to have at least one >=5, all >=5 or unwind and keep those that >=5? Commented Dec 10, 2022 at 13:11
  • Which status should be "OPENED"? Which or how many ratings in the details array need to have a value of 5 or higher? Commented Dec 10, 2022 at 13:13
  • @Takis expected output is count, I mean howmany records are available with matchin conditions. the array to include in condition which have at least one rating >=5 Commented Dec 11, 2022 at 7:08
  • @user20042973 I have updated question, at-least one rating in details should have value >= 5 Commented Dec 11, 2022 at 7:09

1 Answer 1

1
+50

You can try a aggregation query,

  • $match your conditions for leads collection
  • $lookup with pdata collection, pass leadId to match with pdata
  • match required conditions for pdata
  • $limit to return single document, because we don't need that data in response
  • $match condition to check is pdata is not empty
  • $count to get total number of records
db.leads.aggregate([
  {
    $match: {
      status: "OPENED",
      "crossSell.cc.consent": true,
      "crossSell.cc.shown.first": true
    }
  },
  {
    "$lookup": {
      "from": "pdata",
      "let": { "leadId": "$leadId" },
      "pipeline": [
        {
          $match: {
            $expr: { $eq: ["$$leadId", "$activeLeadId"] },
            "details.rating": { $gte: 5 }
          }
        },
        { $limit: 1 }
      ],
      "as": "pdata"
    }
  },
  { $match: { pdata: { $ne: [] } } },
  { $count: "count" }
])

Playground

Sign up to request clarification or add additional context in comments.

6 Comments

'shown' in leads collection and 'details' field in pdata are array. can we use 'details.rating' on array.
This is not giving output. Query is always giving output 1 even for "details.rating": { $gte: 20}
can you provide example documents, that give an output of more than 1 count? you can add documents in the above playground and share the link.
I have added more documents. mongoplayground.net/p/GD6jXrV1MVK
What is the expected count for these documents?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.