Database

db={CUSTOMER:[{"CID":1,"CNAME":"Mark"},{"CID":2,"CNAME":"Chris"},{"CID":3,"CNAME":"James"}],SHOP:[{"Bill_No":1,"TYPE":"SELL","Amount":1000,"CID":1},{"Bill_No":2,"TYPE":"SELL","Amount":350,"CID":2},{"Bill_No":3,"TYPE":"PURCHASE","Amount":450,"CID":1},{"Bill_No":4,"TYPE":"PURCHASE","Amount":360,"CID":3},{"Bill_No":5,"TYPE":"SELL","Amount":800,"CID":3}]}

Query

db.CUSTOMER.aggregate([{$lookup:{from:"SHOP",localField:"CID",foreignField:"CID",as:"TOTAL_ACCOUNTS"}},{"$addFields":{"account_count":{$size:"$TOTAL_ACCOUNTS"}}},{"$project":{_id:0,TOTAL_ACCOUNTS:0}},{$out:"NewCollection"}])

Result