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

微笑、不失礼 提交于 2019-12-12 15:13:47

问题


I found this answer - Answer link

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

I want do same thing but with first 15000 documents in collection, I couldn't find how to apply limit to such query (I tried using $limit : 15000, but it doesn't recognize $limit)

also when I tried -

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

there is no function toArray() for output type cursor.

Guide me how can I accomplish it?


回答1:


Well,
in python, this is how things work - $limit needs to be wrapped in "",
and you need to create a pipeline to execute it as a command.

In my code -

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

You need to wrap everything in double quotes, including your source and destination collection. And in db.command db is the object of your database (ie dbclient.database_name)

As per this answer -

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.

The one that really helped me figure this answer out - Reference 1
And official documentation



来源:https://stackoverflow.com/questions/39959206/save-a-subset-of-mongodb3-0-collection-to-another-collection-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!