MongoDB数据移库

# coding: utf-8

# python setup.py install

import _thread

import time

from pymongo import MongoClient

from pymongo import operations





def get_data(oldmongoip, newmongoip):



    ht_client = MongoClient(oldmongoip, 27017)

    dbs = ht_client.list_database_names()

    for name in dbs:

        if '_2019' in name:

            if 'sxxxs44_2019' != name:

                saveMongo (oldmongoip, newmongoip, name)

    ht_client.close()



    while True:

        time.sleep(1)







def saveMongo(oldmongoip, newmongoip, sname):



    old_client = MongoClient(oldmongoip, 27017)

    new_client = MongoClient(newmongoip, 27017)



    old_db = old_client[sname]

    new_db = new_client[sname]



    coll_names = old_db.list_collection_names(session=None)

    total = 0

    for collection_name in coll_names:

        total += 1

        if total > 0:

            results = old_db[collection_name].find({})

            insertarray = []

            ops = [

猜你喜欢

转载自blog.csdn.net/weixin_42544051/article/details/102969141