首先运行easy_install pymongo
命令安装pymongo驱动。然后执行操作:
创建连接
1 In [1]: import pymongo 2 3 In [2]: connection = pymongo.Connection('localhost', 27017)
切换到数据库malware
In [3]: db = connection.malware
获取collection
1 In [4]: collection = db.malware
注意:db和collection都是延时创建的,在添加Document时才真正创建
Document添加,_id会自动创建
1 In [6]: post = {"name":"a.privacy.GingerMaster.a", "family":"GingMaster", "category":"隐私窃取", "behavior":"非法获取手机root权限,强制开机自启动、强制联网、窃 取并上传用户手机中的IMEI、IMSI、SIM卡信息等隐私内容,还会将病毒组件伪装成PNG图片,从后台静默下载、安装恶意软件,消耗用户流量"} 2 3 In [7]: malinfo = db.posts 4 5 In [9]: malinfo.insert(post) 6 Out[9]: ObjectId('52727c5b3387e31671aa91b1')
获取所有collection(相当于SQL的show tables)
1 In [10]: db.collection_names() 2 Out[10]: [u'system.indexes', u'posts']
获取单个文档
1 In [11]: malinfo.find_one() 2 Out[11]: 3 {u'_id': ObjectId('52727c5b3387e31671aa91b1'), 4 u'behavior': u'u975eu6cd5u83b7u53d6u624bu673arootu6743u9650uff0cu5f3au5236u5f00u673au81eau542fu52a8u3001u5f3au5236u8054u7f51u3001u7a83u53d6u5e76u4e0au4f20u7528u6237u624bu673au4e2du7684IMEIu3001IMSIu3001SIMu5361u4fe1u606fu7b49u9690u79c1u5185u5bb9uff0cu8fd8u4f1au5c06u75c5u6bd2u7ec4u4ef6u4f2au88c5u6210PNGu56feu7247uff0cu4eceu540eu53f0u9759u9ed8u4e0bu8f7du3001u5b89u88c5u6076u610fu8f6fu4ef6uff0cu6d88u8017u7528u6237u6d41u91cf', 5 u'category': u'u9690u79c1u7a83u53d6', 6 u'family': u'GingMaster', 7 u'name': u'a.privacy.GingerMaster.a'}
批量插入
1 In [12]: new_posts = [{"name":"a.payment.FakeInst.a", "family":"FakeInst", "category":"恶意扣费", "behavior":"后台发送扣费短信"}, {"name":"a.payment.Umeng.a", "family":"Umeng", "category":"恶意扣费", "behavior":"1. 后台从服务器端获取指令, 自动发送短信,订制扣费服务,并拦截指定号码短信。 2. 后台从服务器端获取指令,自动模拟访问广告,消耗用户流量"}] 2 3 In [13]: malinfo.insert(new_posts) 4 Out[13]: [ObjectId('527281323387e31671aa91b2'), ObjectId('527281323387e31671aa91b3')]
获取所有collection(相当于SQL的show tables)
1 In [14]: db.collection_names() 2 Out[14]: [u'system.indexes', u'posts']
查询多个文档
1 In [18]: for info in malinfo.find(): 2 ....: print info 3 ....: 4 {u'category': u'u9690u79c1u7a83u53d6', u'_id': ObjectId('52727c5b3387e31671aa91b1'), u'name': u'a.privacy.GingerMaster.a', u'family': u'GingMaster', u'behavior': u'u975eu6cd5u83b7u53d6u624bu673arootu6743u9650uff0cu5f3au5236u5f00u673au81eau542fu52a8u3001u5f3au5236u8054u7f51u3001u7a83u53d6u5e76u4e0au4f20u7528u6237u624bu673au4e2du7684IMEIu3001IMSIu3001SIMu5361u4fe1u606fu7b49u9690u79c1u5185u5bb9uff0cu8fd8u4f1au5c06u75c5u6bd2u7ec4u4ef6u4f2au88c5u6210PNGu56feu7247uff0cu4eceu540eu53f0u9759u9ed8u4e0bu8f7du3001u5b89u88c5u6076u610fu8f6fu4ef6uff0cu6d88u8017u7528u6237u6d41u91cf'} 5 {u'category': u'u6076u610fu6263u8d39', u'_id': ObjectId('527281323387e31671aa91b2'), u'name': u'a.payment.FakeInst.a', u'family': u'FakeInst', u'behavior': u'u540eu53f0u53d1u9001u6263u8d39u77edu4fe1'} 6 {u'category': u'u6076u610fu6263u8d39', u'_id': ObjectId('527281323387e31671aa91b3'), u'name': u'a.payment.Umeng.a', u'family': u'Umeng', u'behavior': u'1. u540eu53f0u4eceu670du52a1u5668u7aefu83b7u53d6u6307u4ee4uff0cu81eau52a8u53d1u9001u77edu4fe1uff0cu8ba2u5236u6263u8d39u670du52a1uff0cu5e76u62e6u622au6307u5b9au53f7u7801u77edu4fe1u3002 2. u540eu53f0u4eceu670du52a1u5668u7aefu83b7u53d6u6307u4ee4uff0cu81eau52a8u6a21u62dfu8bbfu95eeu5e7fu544auff0cu6d88u8017u7528u6237u6d41u91cf'}
加条件的查询
1 In [19]: malinfo.find_one({"family":"FakeInst"}) 2 Out[19]: 3 {u'_id': ObjectId('527281323387e31671aa91b2'), 4 u'behavior': u'u540eu53f0u53d1u9001u6263u8d39u77edu4fe1', 5 u'category': u'u6076u610fu6263u8d39', 6 u'family': u'FakeInst', 7 u'name': u'a.payment.FakeInst.a'}
统计数量
1 In [20]: malinfo.count() 2 Out[20]: 3