-
Notifications
You must be signed in to change notification settings - Fork 235
Aggregation Helper Examples
TylerBrock edited this page Nov 13, 2012
·
1 revision
db.users.insert([
{
"age": 27,
"first_name": "Tyler",
"last_name": "Brock",
"updated": new Date()
},
{
"age": 30,
"first_name": "Jessica",
"last_name": "Fake",
"updated": new Date()
},
{
"age": 35,
"first_name": "Tyler",
"last_name": "Durden",
"updated": new Date()
}
])
db.users.gcount("first_name")
{
"result": [
{
"_id": "Jessica",
"count": 1
},
{
"_id": "Tyler",
"count": 2
}
],
"ok": 1
}
db.users.gcount("first_name", {first_name: "Tyler"})
{
"result": [
{
"_id": "Tyler",
"count": 2
}
],
"ok": 1
}
db.users.gsum("first_name", "age")
{
"result": [
{
"_id": "Jessica",
"sum": 30
},
{
"_id": "Tyler",
"sum": 62
}
],
"ok": 1
}
db.users.gavg("first_name", "age")
{
"result": [
{
"_id": "Jessica",
"avg": 30
},
{
"_id": "Tyler",
"avg": 31
}
],
"ok": 1
}