关于mongo使用及效率

最近一直关注产品的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进行数据的读写操作了。

简单的做一下记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值