将 MongoDB(3.0) 集合的子集保存到 Python 中的另一个集合

Save a subset of MongoDB(3.0) collection to another collection in Python

我找到了这个答案 - Answer link

db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]);

我想做同样的事情,但是收集了前 15000 个文档,我找不到如何对此类查询应用限制(我尝试使用 $limit : 15000,但它不识别 $limit)

我也尝试过 -

db.subset.insert(db.full_set.find({}).limit(15000).toArray())

输出类型 cursor 没有函数 toArray()

指导我如何完成它?

嗯,
在 python 中,事情是这样进行的 - $limit 需要包装在 ""
中 并且您需要创建一个管道以将其作为命令执行。

在我的代码中-

    pipeline = [{ '$limit': 15000 },{'$out': "destination_collection"}]
    db.command('aggregate', "source_collection", pipeline=pipeline)

您需要用双引号将所有内容括起来,包括您的源集合和目标集合。 而在 db.command 中 db 是你数据库的对象(即 dbclient.database_name)

根据这个答案-

It works about 100 times faster than forEach at least in my case. This is because the entire aggregation pipeline runs in the mongod process, whereas a solution based on find() and insert() has to send all of the documents from the server to the client and then back. This has a performance penalty, even if the server and client are on the same machine.

真正帮助我找到答案的人 - Reference 1
并且 official documentation