Pymongo - Fastest way to query all documents
<p>I have a file contain nested dictionary i save on my computer, when i need that file, i used this line of code : </p>

<p><code>u_i = pickle.load(open("F:\\ForTestCode\\user_item_21_07.p", "rb"))</code></p>

<p>This is type data in <code>u_i</code> : </p>

<pre><code>{"BF0E6CF26097FF44B56EBA795F25B48A" : {"557252215845" : 1.0}, 
       "9FFA99A1374CD953A30CB1F746F11C6A" : { "978076659142" : 1.0, "622" : 1.0}
               }
</code></pre>

<p>And then, i inserted nested dictionary to MongoDB, its became like this : </p>

<pre><code>{
    "_id" : ObjectId("57971bcffefb610990cc5eaa"),
    "BF0E6CF26097FF44B56EBA795F25B48A" : {
        "557252215845" : 1.0
    }
}

{
    "_id" : ObjectId("57971bcffefb610990cc5ec6"),
    "9FFA99A1374CD953A30CB1F746F11C6A" : {
        "978076659142" : 1.0,
        "622" : 1.0
    }
}
</code></pre>

<p>So, if i need put all documents in MongoDB into one file like <code>u_i</code> above i need to make a loop like this : </p>

<pre><code>temp_dict = {}
documents_u_i = collection_u_i.find({})
for records in documents_u_i:
   for item in records:
       if item != "_id": # I dont need key and values of `_id`
           temp_dict[item] = records[item]
</code></pre>

<p>And <code>temp_dict</code> will be the nested dictionary like <code>u_i</code> above.</p>

<pre><code>temp_dict = {"BF0E6CF26097FF44B56EBA795F25B48A" : {"557252215845" : 1.0}, 
       "9FFA99A1374CD953A30CB1F746F11C6A" : { "978076659142" : 1.0, "622" : 1.0}
               }
</code></pre>

<p>But i don't want used <code>for</code> loop cause in my db have > 90.000 documents</p>

<p>Is there fastest way to query data without using <code>for</code> loop ?</p>

<p>Thanks very much for help me !</p>