Posted in Python onNovember 01, 2013
首先运行easy_install pymongo命令安装pymongo驱动。然后执行操作:
创建连接
In [1]: import pymongo In [2]: connection = pymongo.Connection('localhost', 27017)
切换到数据库malware
In [3]: db = connection.malware
获取collection
In [4]: collection = db.malware
注意:db和collection都是延时创建的,在添加Document时才真正创建Document添加,_id会自动创建
In [6]: post = {"name":"a.privacy.GingerMaster.a", "family":"GingMaster", "category":"隐私窃取", "behavior":"非法获取手机root权限,强制开机自启动、强制联网、窃 取并上传用户手机中的IMEI、IMSI、SIM卡信息等隐私内容,还会将病毒组件伪装成PNG图片,从后台静默下载、安装恶意软件,消耗用户流量"} In [7]: malinfo = db.posts In [9]: malinfo.insert(post) Out[9]: ObjectId('52727c5b3387e31671aa91b1')
获取所有collection(相当于SQL的show tables)
In [10]: db.collection_names() Out[10]: [u'system.indexes', u'posts']
获取单个文档
In [11]: malinfo.find_one() Out[11]: {u'_id': ObjectId('52727c5b3387e31671aa91b1'), u'behavior': u'\u975e\u6cd5\u83b7\u53d6\u624b\u673aroot\u6743\u9650\uff0c\u5f3a\u5236\u5f00\u673a\u81ea\u542f\u52a8\u3001\u5f3a\u5236\u8054\u7f51\u3001\u7a83\u53d6\u5e76\u4e0a\u4f20\u7528\u6237\u624b\u673a\u4e2d\u7684IMEI\u3001IMSI\u3001SIM\u5361\u4fe1\u606f\u7b49\u9690\u79c1\u5185\u5bb9\uff0c\u8fd8\u4f1a\u5c06\u75c5\u6bd2\u7ec4\u4ef6\u4f2a\u88c5\u6210PNG\u56fe\u7247\uff0c\u4ece\u540e\u53f0\u9759\u9ed8\u4e0b\u8f7d\u3001\u5b89\u88c5\u6076\u610f\u8f6f\u4ef6\uff0c\u6d88\u8017\u7528\u6237\u6d41\u91cf', u'category': u'\u9690\u79c1\u7a83\u53d6', u'family': u'GingMaster', u'name': u'a.privacy.GingerMaster.a'}
批量插入
In [12]: new_posts = [{"name":"a.payment.FakeInst.a", "family":"FakeInst", "category":"恶意扣费", "behavior":"后台发送扣费短信"}, {"name":"a.payment.Umeng.a", "family":"Umeng", "category":"恶意扣费", "behavior":"1. 后台从服务器端获取指令, 自动发送短信,订制扣费服务,并拦截指定号码短信。 2. 后台从服务器端获取指令,自动模拟访问广告,消耗用户流量"}] In [13]: malinfo.insert(new_posts) Out[13]: [ObjectId('527281323387e31671aa91b2'), ObjectId('527281323387e31671aa91b3')]
获取所有collection(相当于SQL的show tables)
In [14]: db.collection_names() Out[14]: [u'system.indexes', u'posts']
查询多个文档
In [18]: for info in malinfo.find(): ....: print info ....: {u'category': u'\u9690\u79c1\u7a83\u53d6', u'_id': ObjectId('52727c5b3387e31671aa91b1'), u'name': u'a.privacy.GingerMaster.a', u'family': u'GingMaster', u'behavior': u'\u975e\u6cd5\u83b7\u53d6\u624b\u673aroot\u6743\u9650\uff0c\u5f3a\u5236\u5f00\u673a\u81ea\u542f\u52a8\u3001\u5f3a\u5236\u8054\u7f51\u3001\u7a83\u53d6\u5e76\u4e0a\u4f20\u7528\u6237\u624b\u673a\u4e2d\u7684IMEI\u3001IMSI\u3001SIM\u5361\u4fe1\u606f\u7b49\u9690\u79c1\u5185\u5bb9\uff0c\u8fd8\u4f1a\u5c06\u75c5\u6bd2\u7ec4\u4ef6\u4f2a\u88c5\u6210PNG\u56fe\u7247\uff0c\u4ece\u540e\u53f0\u9759\u9ed8\u4e0b\u8f7d\u3001\u5b89\u88c5\u6076\u610f\u8f6f\u4ef6\uff0c\u6d88\u8017\u7528\u6237\u6d41\u91cf'} {u'category': u'\u6076\u610f\u6263\u8d39', u'_id': ObjectId('527281323387e31671aa91b2'), u'name': u'a.payment.FakeInst.a', u'family': u'FakeInst', u'behavior': u'\u540e\u53f0\u53d1\u9001\u6263\u8d39\u77ed\u4fe1'} {u'category': u'\u6076\u610f\u6263\u8d39', u'_id': ObjectId('527281323387e31671aa91b3'), u'name': u'a.payment.Umeng.a', u'family': u'Umeng', u'behavior': u'1. \u540e\u53f0\u4ece\u670d\u52a1\u5668\u7aef\u83b7\u53d6\u6307\u4ee4\uff0c\u81ea\u52a8\u53d1\u9001\u77ed\u4fe1\uff0c\u8ba2\u5236\u6263\u8d39\u670d\u52a1\uff0c\u5e76\u62e6\u622a\u6307\u5b9a\u53f7\u7801\u77ed\u4fe1\u3002 2. \u540e\u53f0\u4ece\u670d\u52a1\u5668\u7aef\u83b7\u53d6\u6307\u4ee4\uff0c\u81ea\u52a8\u6a21\u62df\u8bbf\u95ee\u5e7f\u544a\uff0c\u6d88\u8017\u7528\u6237\u6d41\u91cf'}
加条件的查询
In [19]: malinfo.find_one({"family":"FakeInst"}) Out[19]: {u'_id': ObjectId('527281323387e31671aa91b2'), u'behavior': u'\u540e\u53f0\u53d1\u9001\u6263\u8d39\u77ed\u4fe1', u'category': u'\u6076\u610f\u6263\u8d39', u'family': u'FakeInst', u'name': u'a.payment.FakeInst.a'}
统计数量
In [20]: malinfo.count() Out[20]: 3
python操作MongoDB基础知识
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@