First run the Easy_install Pymongo command to install the Pymongo driver. Then do the action:
Create a connection
Copy Code code as follows:
In [1]: Import Pymongo
In [2]: Connection = Pymongo. Connection (' localhost ', 27017)
Switch to database malware
Copy Code code as follows:
In [3]: db = Connection.malware
Get collection
Copy Code code as follows:
In [4]: Collection = Db.malware
Note: Both DB and collection are created by delay, and the document addition is created when the document is added, _id automatically creates
Copy Code code as follows:
In [6]: Post = {"name": "A.privacy.gingermaster.a", "Family": "Gingmaster", "category": "Privacy Theft", "behavior": "Illegal access to cell phone root" , forced to boot, forced networking, steal and upload the user's phone IMEI, IMSI, SIM card information and other privacy content, but also the virus components disguised as PNG image, from the background silently download, install malicious software, consumption of user traffic "}
In [7]: Malinfo = db.posts
In [9]: Malinfo.insert (POST)
OUT[9]: ObjectId (' 52727c5b3387e31671aa91b1 ')
Get all collection (equivalent to SQL show tables)
Copy Code code as follows:
In [ten]: Db.collection_names ()
OUT[10]: [u ' system.indexes ', U ' posts ']
Get a single document
Copy Code code as follows:
In [one]: 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 '}
BULK INSERT
Copy Code code as follows:
in [[]: New_posts = [{"Name": "A.payment.fakeinst.a", "Family": "Fakeinst", "category": "Malicious deduction fee", "behavior": "Backstage send the deduction fee SMS"}, {"Name": "A.payment.umeng.a", "Family": "Umeng", "category": "Malicious deduction fee", "Behavior": "1. Background from the server to obtain instructions, automatic SMS, custom deduction service, and intercept the specified number of text messages. 2. Background from the server to obtain instructions, automatic simulation of access to ads, consumption of user traffic "}]
in [[]: Malinfo.insert (new_posts)
OUT[13]: [ObjectId (' 527281323387e31671aa91b2 '), ObjectId (' 527281323387e31671aa91b3 ')]
Get all collection (equivalent to SQL show tables)
Copy Code code as follows:
In []: Db.collection_names ()
OUT[14]: [u ' system.indexes ', U ' posts ']
Querying multiple documents
Copy Code code as follows:
In [%]: 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 '}
Conditional query
Copy Code code as follows:
in [[]: 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 '}
Number of statistics
Copy Code code as follows:
In [m]: Malinfo.count ()
OUT[20]: 3