I m working with MongoDB but i m facing problem with $project not displaying the data after $group is executed .
My database look like this
# In DATABASE
{
"_id": 8144,
"merchant_id": 8144,
"name": "Google",
"email": "google-world@gmail.com",
}
{
"_id": 2,
"merchant_id": 2,
"name": "LENOVO",
"email": "lenovo-world@gmail.com",
"submerchant_id": 8144,
}
{
"_id": 3,
"merchant_id": 3,
"name": "HP",
"email": "hp-world@gmail.com",
"submerchant_id": 8144,
}
{
"_id": 4,
"merchant_id": 4,
"name": "DELL",
"email": "dell-world@gmail.com",
"submerchant_id": 8144,
}
The output I want is to display the details of the merchant along with count of submerchants
#Output
{
"_id": 8144,
"merchant_id": 8144,
"name": "Google",
"email": "google-world@gmail.com",
"count": 3,
# merchant_id : 8144 has 3 submerchants (Lenovo,hp,dell)
}
MY QUERY is below
db.merchant_info.aggregate([
{ '$group': {'_id': '$submerchant_id', 'count': {'$sum': 1}}},
{ '$project': {'merchant_id': 1, 'name': 1, 'email': 1,'count' :1} }])
BUT I m getting the output has this
{ _id: 8144, count: 3 }
Why is it not showing the merchant_id,name,email data ? Where am i going wrong please let me know ?