0

I have two collection present in mongodb database. one status column is common. I need matched and unmatched count from both the collection based on status column. I have written some code but it fetching only matched count.

db.properties.aggregate([
    {
        $lookup: {
            from: "old_properties_data",
            localField: "identifier",
            foreignField: "identifier",
            as: "col2docs"
        }
    },
    
  
    {"$group" : {_id:"_id", count:{$sum:1}}}
])
Coder
  • 184
  • 15

1 Answers1

0

You can first $group by identifier to get the total match count. Then You can use uncorreleated subquery in $lookup to get the total size of old_properties_data. Finally do a $subtract to get the total unmatched count.

Here is the Mongo Playground for your reference.

ray
  • 11,310
  • 7
  • 18
  • 42
  • Thank you for the Mongo Playground code. but i need complete group of matched and unmatched count with respect to identifier. like if identifier match with any identifier in other collection so match count increased and the total unmatched count is total count - matched count – Coder Jul 15 '21 at 05:41
  • updated the answer and playground link. You may have a look to see if thats what your are looking for :) – ray Jul 15 '21 at 06:16