1. Aggregation function
- Total number of queries. COUNT ()
>Db.userinfo.count ()3> Db.userinfo.Find(){ "_id":1,"name":"Uncle Guo","Sex":"male"," Age":" the" }{ "_id":2,"name":"Miss Guo","Sex":"male"," Age":"Unknown" }{ "_id":3,"name":"Master Guo","Sex":"male"," Age":" -"}
2. Aggregate () method
Let's insert some test data first.
{ "_id": ObjectId ("5abc960c684781cda6d38027"), "Name": "Uncle Guo", "Sex": "Male", "Age": 80.0, "Skil" : [ "PHP", "Js", "MYSQL", "Html", "Css" ]}{ "_id": ObjectId ("5abc960c684781cda6d38028"), "Name": "Uncle Guo", "Sex": "Male", "Age": 60.0, "Skil" : [ "PHP", "MYSQL" ]}{ "_id": ObjectId ("5abc960c684781cda6d38029"), "Sex": "Female"}{ "_id": ObjectId ("5abc960c684781cda6d3802a"), "Name": "Master Guo", "Sex": "Male", "Age": 18.0, "Skil" : [ "Css", "Java", "Html" ]}
Check the sum of all ages
db.userinfo.aggregate ([ {$group: { null, ' $age '}} }])
Results
{ null, "age_sum": 158.0}
- Average
- Minimum value
- Maximum Value
- Get the first document
- Get the last document
MongoDB Aggregation Pipeline (aggregate)