MongoDB Pymongo将整个集合复制到另一个集合



我在一个集合中有10000个数据,希望将整个数据复制到另一个集合。

我是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((时,它会打印出数据。

有什么建议吗?

您尝试过使用db.command()函数吗?这样的东西应该行得通。

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

最新更新