Skip to content

Instantly share code, notes, and snippets.

@const-g
Created December 11, 2015 06:37
Show Gist options
  • Save const-g/925dcbd07deb409e24c7 to your computer and use it in GitHub Desktop.
Save const-g/925dcbd07deb409e24c7 to your computer and use it in GitHub Desktop.
test> db.salary.find()
{
"_id": ObjectId("5669fee5cdeb623e44ea3f41"),
"emp_id": 3,
"Employee_Name": "Farquleet Shadab",
"Employee_Gender": "Male",
"Employee_Salary": [
{
"month_year": "Jan-2015",
"salary_paid": 100
},
{
"month_year": "Feb-2015",
"salary_paid": 100
}
]
}
{
"_id": ObjectId("566a6e9a35ed09e99590b9ab"),
"emp_id": 4,
"Employee_Name": "John Robert",
"Employee_Gender": "Male",
"Employee_Salary": [
{
"month_year": "Jan-2015",
"salary_paid": 200
},
{
"month_year": "Feb-2015",
"salary_paid": 200
}
]
}
// aggregate:
db.salary.aggregate([
{
$unwind: "$Employee_Salary"
}
,{
$match:
{
"Employee_Salary.month_year" : "Jan-2015"
}
}
,{
$group:
{
_id:null,
total: { $sum: "$Employee_Salary.salary_paid" }
}
}
])
// Result:
{
"waitedMS": NumberLong("0"),
"result": [
{
"_id": null,
"total": 300
}
],
"ok": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment