基本操作
基本查询
以更美观的方式输出
|
|
条件查询
条件操作符
|
|
比如查询uid大于100的一些记录
|
|
判断字段是否存在
|
|
判断字段值是否为空
|
|
获取数据库的所有聚合名称
|
|
统计数据库特定聚合所有记录数量
|
|
获取数据库特定聚合的索引
比如获取phone_number聚合的所有索引
1234567891011 > db.phone_number.getIndexes()[{"v" : 1,"key" : {"_id" : 1},"name" : "_id_","ns" : "blacklist.phone_number"}]
添加索引
为phone_number的phone字段添加索引
1234567 > db.phone_number.ensureIndex({"phone":1}){"createdCollectionAutomatically" : false,"numIndexesBefore" : 1,"numIndexesAfter" : 2,"ok" : 1}再查看索引情况验证一下
12345678910111213141516171819 > db.phone_number.getIndexes()[{"v" : 1,"key" : {"_id" : 1},"name" : "_id_","ns" : "blacklist.phone_number"},{"v" : 1,"key" : {"phone" : 1},"name" : "phone_1","ns" : "blacklist.phone_number"}]
分组查询group count
|
|
如果需要排序的话
123456789101112131415161718192021 > db.phone.aggregate([{$group:{_id:"$source", count:{$sum:1}}},{$sort:{"count":-1}}]){ "_id" : "other", "count" : 6133225 }{ "_id" : "ly.com", "count" : 5235378 }{ "_id" : "zmyzm.com", "count" : 1038465 }{ "_id" : "f02.cn", "count" : 644959 }{ "_id" : "shjmpt.com", "count" : 502440 }{ "_id" : "yzm1", "count" : 449854 }{ "_id" : "yma0.com", "count" : 417669 }{ "_id" : "6yzm.com", "count" : 297152 }{ "_id" : "taomapt.com", "count" : 187613 }{ "_id" : "83r.com", "count" : 144898 }{ "_id" : "kuaima9", "count" : 138903 }{ "_id" : "creditease", "count" : 101747 }{ "_id" : "tongdun", "count" : 95012 }{ "_id" : "aliqin", "count" : 87845 }{ "_id" : "maz8", "count" : 81461 }{ "_id" : "xudan123.com", "count" : 74808 }{ "_id" : "ema6", "count" : 54819 }{ "_id" : "feiq", "count" : 46670 }{ "_id" : "sunvarma.com", "count" : 45164 }{ "_id" : "yayayama.com", "count" : 44941 }
MongoDB 唯一索引自动删除重复记录
|
|
Python 相关
|
|
mongodb.config
|
|
导入json文件数据
将json数据导入到blacklist的phone_number集合中
多实例配置
如果想同一台机器跑多个MongoDB,则在启动Mongo的时候指定不同的端口即可,如
|
|
要使用第二个Mongo,加上端口就好
|
|