4. Example
{
title : “mongo DB " ,
author : "bob" ,
posted : new Date() ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
language : “English”,
other : { foo : 5 }
}
5. 1. Count – return number of documents in collection.
> db.user.count();
you can also pass query, and MongoDB will count number of result for
that query.
2. Distinct
The distinct commands find all the distinct values for given key in
collection.
db.runCommand ( {“distinct” : “collection name” ,”key” : “key field” } )
3. Match
>db.blog.aggregate({$match : {pageViews : {$gt : 1 ,$lt :50 } } } )
4. Limit
> db.blog.aggregate({$limit : 5 } )
5. Sort
>db.blog.find( ).sort({title : 1} )
6. 6. $project
Include, exclude or rename fields
Insert computed fields
Create sub-document fields
selecting and excluding fields
>db.blog.aggregate{
{
“$project” : { _id : 0 , title : 1 , language : 1}
}
}
9. Rename Page Views
db.blog.aggregate( { title : 1,
newPageViews : “$pageViews”
} )
Create and Populate new Field that hold subfields
db.blog.aggregate( {"$project" : {
title : 1 ,
stats:{pv : "$pageViews" ,
foo : "$other.foo" }
}
})
10. Groups
Each document in collection has following form
{
"_id": “413710",
"city": “Pune",
"state": “MH",
"pop": 5574,
"loc": [ -74.016323, 40.710537 ]
}