1 下载
https://www.mongodb.com/download-center/community
比如:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz
2 连接
# cd $MONGODB_HOME
# bin/mongo master:27017/database_name
3 查询
1 count
>db.getCollection('table_name').find({}).count()
2 group by
>db.getCollection('table_name').aggregate([{"$group": {_id: "$column_name", count: {"$sum": 1}}}])
3 select by limit
>db.getCollection('table_name').find({}).limit(1).pretty()
4 select by condition ‘=’
>db.getCollection('table_name').find({"column_name": "column_value"})
5 select by condition ‘>’
>db.getCollection('table_name').find({"column_name":{$gt:"column_value"}}).pretty()
6 select by condition ‘or’
>db.getCollection('table_name').find({$or: [ {"column_name1":"column_value1"},{"column_name2":{$gt:"column_value2"}}]}).pretty()
7 select by date range
>db.getCollection('table_name').find({"create_time":{$gte:ISODate("2019-05-18T00:00:00Z"),$lt:ISODate("2019-05-19T00:00:00Z")}})
8 map reduce - 词频统计
统计信息
>db.table_name.mapReduce(function(){ emit(this.column,1);}, function(key, values){return Array.sum(values);}, {out:"post_total"})
输出结果
>db.table_name.mapReduce(function(){ emit(this.column,1);}, function(key, values){return Array.sum(values);}, {out:"post_total"}).find()
默认只输出20行结果,然后需要不断输入it才能输出更多结果,如果想一次输出全部结果,可以先执行
>DBQuery.shellBatchSize = 100000;