Run the easy_install pymongo command to install the pymongo driver. Then perform the following operations:
Create a connection
Copy codeThe Code is as follows:
In [1]: import pymongo
In [2]: connection = pymongo. Connection ('localhost', 27017)
Switch to the database malware
Copy codeThe Code is as follows:
In [3]: db = connection. malware
Get collection
Copy codeThe Code is as follows:
In [4]: collection = db. malware
Note: db and collection are created in a delayed manner. The Document is added only when the Document is added. _ id is automatically created.
Copy codeThe Code is as follows:
In [6]: post = {"name": ". privacy. gingerMaster. a "," family ":" GingMaster "," category ":" Privacy theft "," behavior ":" illegal access to the root permission of the mobile phone, force start, force Internet connection, steal and upload private content such as IMEI, IMSI, and SIM card on your mobile phone, and disguise the virus components as PNG images, silently downloads and installs malware from the background, consuming user traffic "}
In [7]: malinfo = db. posts
In [9]: malinfo. insert (post)
Out [9]: ObjectId ('52727c5b3387e000071aa91b1 ')
Get all collections (equivalent to SQL show tables)
Copy codeThe Code is as follows:
In [10]: db. collection_names ()
Out [10]: [u'system. indexes', u'posts']
Obtain a single document
Copy codeThe Code is as follows:
In [11]: malinfo. find_one ()
Out [11]:
{U' _ id': ObjectId ('52727c5b3387e000071aa91b1 '),
U'behavior ': u' \ u975e \ Users \ u83b7 \ u53d6 \ Users \ u6743 \ u9650 \ Users \ u5236 \ Users \ u673a \ u81ea \ u542f \ u52a8 \ u3001 \ u5f3a \ u5236 \ u8054 \ u7f51 \ u3001 \ u7a83 \ u53d6 \ Alibaba \ Users \ u7528 \ u6237 \ Users \ u673a \ Users \ u3001SIM \ u5361 \ u4fe1 \ u606f \ u9690 \ u79c1 \ u5185 \ u5bb9 \ Alibaba \ u75c5 \ Alibaba \ u88c5 \ u6210PNG \ u56fe \ u7247 \ uff0c \ u4ece \ u540 \ u53f0 \ u9759 \ u9ed8 \ Users \ u8f7d \ u3001 \ Users \ u88c5 \ u6076 \ Users \ u8017 \ u7528 \ u6237 \ u6d41 \ u91cf ',
U'category ': U' \ u9690 \ u79c1 \ u7a83 \ u53d6 ',
U'family ': u'gingmaster ',
U'name': u'a. privacy. GingerMaster. '}
Batch insert
Copy codeThe Code is as follows:
In [12]: new_posts = [{"name": ". payment. fakeInst. a "," family ":" FakeInst "," category ":" malicious fee deduction "," behavior ":" text message "},{" name ": ". payment. umeng. a "," family ":" Umeng "," category ":" malicious fee deduction "," behavior ":" 1. the backend obtains commands from the server, automatically sends text messages, customizes fee deduction services, and intercepts text messages of specified numbers. 2. the backend obtains commands from the server, automatically simulates access advertisements, and consumes user traffic. "}]
In [13]: malinfo. insert (new_posts)
Out [13]: [ObjectId ('527281323387e000071aa91b2 '), ObjectId ('527281323387e000071aa91b3')]
Get all collections (equivalent to SQL show tables)
Copy codeThe Code is as follows:
In [14]: db. collection_names ()
Out [14]: [u'system. indexes', u'posts']
Query multiple documents
Copy codeThe Code is as follows:
In [18]: for info in malinfo. find ():
...: Print info
....:
{U'category ': U' \ u9690 \ u79c1 \ u7a83 \ u53d6', U' _ id': ObjectId ('52727c5b3387e000071aa91b1 '), u 'name': u 'a. privacy. gingerMaster. a', u'family ': u 'gingmaster', u 'behavior ': u' \ u975e \ Users \ u83b7 \ u53d6 \ Users \ u6743 \ u9650 \ Users \ u5236 \ Users \ u673a \ u81ea \ u542f \ u52a8 \ u3001 \ u5f3a \ u5236 \ u8054 \ u7f51 \ u3001 \ u7a83 \ u53d6 \ Alibaba \ Users \ u7528 \ u6237 \ Users \ u673a \ Users \ u3001SIM \ u5361 \ u4fe1 \ u606f \ u9690 \ u79c1 \ u5185 \ u5bb9 \ Alibaba \ u75c5 \ Alibaba \ u88c5 \ u6210PNG \ u56fe \ u7247 \ uff0c \ u4ece \ u540 \ u53f0 \ u9759 \ u9ed8 \ Alibaba \ u8f7d \ u3001 \ Alibaba \ u88c5 \ u6076 \ Alibaba \ u8017 \ u7528 \ u6237 \ u6d41 \ u91cf '}
{U'category ': U' \ u6076 \ u610f \ u6263 \ u8d39', U' _ id': ObjectId ('527281323387e000071aa91b2 '), 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 ('527281323387e000071aa91b3 '), u 'name': u'a. payment. umeng. a', u'family ': u'umeng', u'behavior': u'1. \ Users \ u53f0 \ u4ece \ u670d \ u52a1 \ u5668 \ u7aef \ u83b7 \ u53d6 \ Alibaba \ Users \ u81ea \ u52a8 \ u53d1 \ u9001 \ u77ed \ Users \ u5236 \ Users \ u8d39 \ u670d \ u52a1 \ uff0c \ u5e76 \ u62e6 \ u622a \ u6307 \ u5b9a \ u53f7 \ u7801 \ u77ed \ Users \ u3002. \ Users \ u53f0 \ u4ece \ Users \ u52a1 \ u5668 \ u7aef \ u83b7 \ u53d6 \ u6307 \ Users \ u81ea \ u52a8 \ Users \ u62df \ Users \ u95ee \ Users \ u544a \ uff0c \ u6d88 \ u8017 \ u7528 \ u6237 \ u6d41 \ u91cf '}
Conditional Query
Copy codeThe Code is as follows:
In [19]: malinfo. find_one ({"family": "FakeInst "})
Out [19]:
{U' _ id': ObjectId ('527281323387e000071aa91b2 '),
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. '}
Count
Copy codeThe Code is as follows:
In [20]: malinfo. count ()
Out [20]: 3