Basic knowledge of python operation MongoDB

  • 2020-04-02 13:03:49
  • OfStack

First, run the easy_install pymongo command to install the pymongo driver. Then perform the following actions:
Create a connection


In [1]: import pymongo
In [2]: connection = pymongo.Connection('localhost', 27017)

Switch to database malware

In [3]: db = connection.malware

Access to the collection

 In [4]: collection = db.malware

Note: db and collection are created in a delayed manner. The Document addition is only created when the Document is added, and the _id is created automatically

In [6]: post = {"name":"a.privacy.GingerMaster.a", "family":"GingMaster", "category":" Privacy to steal ", "behavior":" Illegal acquisition of mobile phone root Permission, forced boot from the start, forced networking, theft   Take and upload from the user's phone IMEI , IMSI , SIM Private content, such as card information, can also disguise the virus components as PNG Picture, silently download from the background, install malware, consume user traffic "}
In [7]: malinfo = db.posts
In [9]: malinfo.insert(post)
Out[9]: ObjectId('52727c5b3387e31671aa91b1')

Get all collections (equivalent to show tables in SQL)

In [10]: db.collection_names()
 Out[10]: [u'system.indexes', u'posts']

Get a single document

In [11]: malinfo.find_one()
Out[11]:
{u'_id': ObjectId('52727c5b3387e31671aa91b1'),
 u'behavior': u'u975eu6cd5u83b7u53d6u624bu673arootu6743u9650uff0cu5f3au5236u5f00u673au81eau542fu52a8u3001u5f3au5236u8054u7f51u3001u7a83u53d6u5e76u4e0au4f20u7528u6237u624bu673au4e2du7684IMEIu3001IMSIu3001SIMu5361u4fe1u606fu7b49u9690u79c1u5185u5bb9uff0cu8fd8u4f1au5c06u75c5u6bd2u7ec4u4ef6u4f2au88c5u6210PNGu56feu7247uff0cu4eceu540eu53f0u9759u9ed8u4e0bu8f7du3001u5b89u88c5u6076u610fu8f6fu4ef6uff0cu6d88u8017u7528u6237u6d41u91cf',
 u'category': u'u9690u79c1u7a83u53d6',
 u'family': u'GingMaster',
 u'name': u'a.privacy.GingerMaster.a'}

Bulk insert

In [12]: new_posts = [{"name":"a.payment.FakeInst.a", "family":"FakeInst", "category":" Malicious deduction ", "behavior":" Send deduction SMS in the background "}, {"name":"a.payment.Umeng.a", "family":"Umeng", "category":" Malicious deduction ", "behavior":"1.  The background gets instructions from the server side,   Automatic sending SMS, custom deduction service, and intercept the specified number of SMS.  2.  Background from the server to obtain instructions, automatically simulate access to advertising, consumption of user traffic "}]
In [13]: malinfo.insert(new_posts)
Out[13]: [ObjectId('527281323387e31671aa91b2'), ObjectId('527281323387e31671aa91b3')]

Get all collections (equivalent to show tables in SQL)

In [14]: db.collection_names()
Out[14]: [u'system.indexes', u'posts']  

Querying multiple documents

In [18]: for info in malinfo.find():
   ....:     print info
   ....:     
{u'category': u'u9690u79c1u7a83u53d6', u'_id': ObjectId('52727c5b3387e31671aa91b1'), u'name': u'a.privacy.GingerMaster.a', u'family': u'GingMaster', u'behavior': u'u975eu6cd5u83b7u53d6u624bu673arootu6743u9650uff0cu5f3au5236u5f00u673au81eau542fu52a8u3001u5f3au5236u8054u7f51u3001u7a83u53d6u5e76u4e0au4f20u7528u6237u624bu673au4e2du7684IMEIu3001IMSIu3001SIMu5361u4fe1u606fu7b49u9690u79c1u5185u5bb9uff0cu8fd8u4f1au5c06u75c5u6bd2u7ec4u4ef6u4f2au88c5u6210PNGu56feu7247uff0cu4eceu540eu53f0u9759u9ed8u4e0bu8f7du3001u5b89u88c5u6076u610fu8f6fu4ef6uff0cu6d88u8017u7528u6237u6d41u91cf'}
{u'category': u'u6076u610fu6263u8d39', u'_id': ObjectId('527281323387e31671aa91b2'), u'name': u'a.payment.FakeInst.a', u'family': u'FakeInst', u'behavior': u'u540eu53f0u53d1u9001u6263u8d39u77edu4fe1'}
{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'}

Conditional queries

In [19]: malinfo.find_one({"family":"FakeInst"})
Out[19]:
{u'_id': ObjectId('527281323387e31671aa91b2'),
 u'behavior': u'u540eu53f0u53d1u9001u6263u8d39u77edu4fe1',
 u'category': u'u6076u610fu6263u8d39',
 u'family': u'FakeInst',
 u'name': u'a.payment.FakeInst.a'}

Statistics of the number

In [20]: malinfo.count()
Out[20]: 3

 


Related articles: