Save a subset of MongoDB(3.0) collection to another collection in Python
<p>I found this answer - <a href="http://stackoverflow.com/a/25247084/609782">Answer link</a></p>

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

<p>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 <code>$limit : 15000</code>, but it doesn't recognize $limit)</p>

<p>also when I tried - </p>

<pre><code>db.subset.insert(db.full_set.find({}).limit(15000).toArray())
</code></pre>

<p>there is no function <code>toArray()</code> for output type <code>cursor</code>.
<br><br>Guide me how can I accomplish it?</p>

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

<p>In my code -</p>

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

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

<p>As per this answer -</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>The one that really helped me figure this answer out - <a href="http://blog.pythonisito.com/2012/06/using-mongodbs-new-aggregation.html" rel="nofollow">Reference 1</a>
<br>
And <a href="https://docs.mongodb.com/v3.2/aggregation/" rel="nofollow">official documentation</a></p>

python mongodb mongodb-query aggregation-framework mongodb-aggregation