Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use mongodb Module in Python

Shulou Source: shulou.com Published: 2022-05-31 21:15:55 09月16日 Update

This article introduces how to use the mongodb module in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

When it comes to python and mongodb, then installing the corresponding modules is essential, the easiest way to install, or it must be pip.

# pip install pymongo==3.0.4

By the way, record how the source code is installed.

# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.8.tar.gz#md5=23100361c9af1904eb2d7722f2658114-- no-check-certificate# tar xf pymongo-2.8.tar.gz# cd pymongo-2.8# python setup.py install

Taken from a log.

35783 s100 android 47 5 192.168.1.100 2015-09-05 08:03:19 strengthenHeroByHeroes {"consume_gold": {"ogold": 2893821, "cgold": 1700, "gold": 2892121, "tag": "strengthenHeroByHeroes"}, "taskInfo": [{"id": 2310033, "progress": 2, "status": 0}], "delHeroList": {"id": 102014, "id": 102010, "id": 102010}, "id": 100026 "olevel": 46, "oexp": 1700, "cexp": 1700, "level": 46, "exp": 3400} 865982021462182 XiaoMi

Python script for storing mongodb

[root@localhost opt] # cat analytical.py #! / usr/bin/env python#coding:utf8import os,sys,jsonfrom datetime import * from pymongo import MongoClientdef ConMongo (host,port,cur_db,username,password): client = MongoClient (host,port) db = client [cur _ db] db.authenticate (username,password) table = db.gamelogs return tabledef parseLog (logfile) Table): dic = {} dl = [] with open (file_log) as fd: for line in fd: try: tokens = line.strip () .split ('\ t') uid = tokens [0] server = tokens [1] system = tokens [2] level = int (tokens [3]) vip_level = tokens [4] ip = tokens [5] time = datetime.strptime (tokens [6] "% Y-%m-%d% H:%M:%S") # convert the time string to the time format action = tokens [7] result = json.loads (tokens [8]) # Special string to json format uuid = tokens [ 9] if len (tokens) = = 12: channel = tokens [11] else: channel =''dic = {' uid':uid 'server':server,'system':system,'level':level,'vip_level':vip_level,'ip':ip,'time':time,'action':action,'result':result,'uuid':uuid 'channel':channel} dl.append (dic) if len (dl) = = 10000: table.insert_many (dl) dl = [] except Exception,e: print e Line if len (dl) > 0: table.insert_many (dl) if _ _ name__ ='_ _ main__': table = ConMongo ('localhost',27017,'talefundb','talefun','123456') try: logfile = sys.argv [1] parseLog (logfile,table) except IndexError,e: print e

Note:

(1) the insert_many parameter is added in mongodb 3.0.4, which allows you to insert a large list directly into the mongodb database. (2) the script makes restrictions. If there are 2000 values in the dictionary, insert the data once into mongodb. This ensures the efficiency. (3) it is not recommended to copy the script test directly. After many tabs are pasted out, problems will occur. I will put the script on the cloud disk and you can download it and test it.

On how to use the mongodb module in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Scripts modules content characters strings data time more formats help tests good special items interests tabs parameters dictionaries boys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Shulou Tech Info macOS vpn