【问题标题】:MongoDB Pymongo copy a whole collection to anotherMongoDB Pymongo 将整个集合复制到另一个
【发布时间】:2021-06-14 07:29:35
【问题描述】:

我在一个集合中有 10,000 个数据,想将整个数据复制到另一个集合。

我是 mongodb 的新手,在这里被困了一段时间,正在寻求帮助。

我试过了

for a in db.source_file.find():
        try:
            db.destination.insert(a) // tried insert_one here too
        except:
            print('did not copy')

没有复制,一直打印出“没有复制”

我也试过了

SOURCE = db.source_file
DESTINATION = db.destination

pipeline = [ {"$match": {}}, 
             {"$out": "DESTINATION" },
]

SOURCE.aggregate(pipeline)

这也没有复制任何东西

源集合肯定包含数据,因为当我尝试 source_col.find_one() 时,它会打印出数据。

有什么建议吗?

【问题讨论】:

    标签: mongodb pymongo


    【解决方案1】:

    您是否尝试过使用db.command() 函数? 这样的事情应该可以工作。

    db.command('eval', 'db.collection.copyTo("newcollection")', nolock=True)
    

    【讨论】:

    • 我脑子里突然冒出另一件事。您是否尝试过打印错误以查看问题所在? ``` try: #some code here ... 除了 Error as err: print(str(err)) ```
    猜你喜欢
    • 2017-02-08
    • 2023-03-13
    • 2023-01-10
    • 1970-01-01
    • 2015-10-23
    • 2020-12-25
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多