最近一直关注产品的mongo数据删除速率。
因为存储关联的原因,数据删除效率低效。产品使用的mongo版本是2.6,跟踪mongo官网,发现mongo对引擎进行了升级。2.x-3版本的巨大变化:
关于升级的改动,就不一一赘述,一张图说明了一切。
了解一个产品的最好方法是了解其官方的说明文档。希望大家可以多多关注官网上的信息去了解。
mongo对文件的存储也是极好的,尤其是针对>16M的文件。
GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片、音频、视频等)。
Python中关于mongo的使用及其简单,主要使用了pymongo包:
import gridfs
import pymongo
import socket
class Mongo(object):
def __init__(self):
self.client = None
self.db = None
self.enabled = None
self.hostname = None
self.port = None
self.database = None
self.username = None
self.password = None
self.grid = None
def init(self):
self.enabled = True
self.hostname = '127.0.0.1'
self.port = 27017
self.database = 'useful'
self.username = ''
self.password = ''
return self.enabled
def drop(self):
self.client.drop_database(self.database)
def close(self):
self.client.close()
def connect(self):
if not self.enabled:
return
# Warn the user that this may take a while if an instant connection
# could not be made with the MongoDB server.
try:
socket.create_connection((self.hostname, self.port), 1).close()
except socket.error:
print(
"We're attempting to connect to MongoDB, but the connection "
"seems slow or MongoDB is simply offline. Please wait while "
"tries to connect.."
)
try:
self.client = pymongo.MongoClient(self.hostname, self.port)
self.db = self.client[self.database]
if self.username and self.password:
self.db.authenticate(self.username, self.password)
self.grid = gridfs.GridFS(self.db)
# Fetch the collection names to force Mongo to connect.
self.collection_names = self.db.collection_names()
except pymongo.errors.PyMongoError as e:
raise UseCriticalError(
"Unable to connect to MongoDB: %s. In order to operate "
"is required." % e
)
mongo = Mongo()
if not mongo.init():
print("mongo not start...\nPlease start mongo...")
mongo.connect()
连接后就可以直接对mongo进行数据的读写操作了。
简单的做一下记录。