copy_mongo.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:airbnb-bot 作者: weineran 项目源码 文件源码
def mongo_do_bulk_insert(target_collection, documents_to_insert):
    assert isinstance(target_collection, collection.Collection)
    assert isinstance(documents_to_insert, cursor.Cursor)

    print("Doing bulk insert of [%s] documents into destination [%s]" % (
        documents_to_insert.count(), target_collection.database.name + "." + target_collection.name))

    try:
        result = target_collection.insert_many(documents_to_insert)
    except BulkWriteError as bwe:
        pprint(bwe.details)
        exit()

    inserted_count = len(result.inserted_ids)

    if inserted_count == documents_to_insert.count():
        print("Successfully inserted all [%d] documents." % inserted_count)
    elif inserted_count < documents_to_insert.count():
        print("Not all insertions succeeded.  Inserted [%d] out of [%d] documents." % (
            inserted_count, documents_to_insert.count()))
    else:
        print("ERROR: Inserted [%d] documents, which is more than documents_to_insert.count() [%d]." % (
            inserted_count, documents_to_insert.count()))
        exit()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号