2

Running the following mongo query in RoboMongo (0.9.0-RC09) gives the correct number of documents (using the cursors count function), while iterating all documents does only return a small portion of documents:

var allDocuments = db.getCollection('mycollection').find({});
print(allDocuments.size());  // prints 170 000 -> correct

var count = 0;
allDocuments.forEach(function(doc) {
    count++;
});
print(count); // 'randomly' prints values between 30 000 and 44 000

Do we need to specifically configure the query to return all documents?

hupfis
  • 128
  • 7

2 Answers2

4

Problem solved: This is an issue of robomongo shellTimeoutSec configuration (default: 15 seconds) which caused the curser to stop returning more elements.

This also explains the 'random' count of 30 000 to 44 000 (depending on network speed). Here is the ticket of robomogo: https://github.com/paralect/robomongo/issues/1106#issuecomment-230258348

The fix/workaround for now is to increase shellTimeoutSec in robomongo.json:

Windows
 0.9.x
  C:\Users\<user>\.config\robomongo\0.9\robomongo.json
 0.8.x
  C:\Users\<user>\.config\robomongo\robomongo.json   
MAC
 0.9.x
  /Users/<user>/.config/robomongo/0.9/robomongo.json
 0.8.x
  /Users/<user>/.config/robomongo/robomongo.json     
Linux
 0.9.x
  /home/<user>/.config/robomongo/0.9/robomongo.json
 0.8.x
  /home/<user>/.config/robomongo/robomongo.json
hupfis
  • 128
  • 7
  • Thank you! Was driving me nuts when I realised not all updates had been completed, but no diagnostics/warnings - wtf?? – randomsock Feb 01 '18 at 19:36
-2

We need to convert into array. After that only we can do forEach. Try Below !!!

var allDocuments = db.getCollection('mycollection').find({}).toArray();
print(allDocuments.length);  
var count = 0;
allDocuments.forEach(function(doc) {
count++;
print("IterCount : ",count); 
});
print("FinalCount : ",count); 

// With Cursor

db.getCollection('mycollection').find({}).forEach(function(doc){
count++;
print("IterCount : ",count);});
Selva Kumar
  • 839
  • 1
  • 10
  • 15
  • forEach is defined on the cursor and should work, as document in https://docs.mongodb.com/manual/reference/method/cursor.forEach/ (we use mongodb 3.2.6) – hupfis Sep 27 '16 at 07:09
  • This suggestion will load all the data in memory. Not necessarily a good idea. – qqilihq Sep 27 '16 at 07:23
  • Hi Hupfis ! the cursor will immediately closed once the operation done. It works after some time the cursor close so you can't get result.I will edit the answer you try out this – Selva Kumar Sep 27 '16 at 07:45
  • In your code your assign your cursor result into variable.so it will expired once your mongodb operation done. – Selva Kumar Sep 27 '16 at 07:50
  • So try second one in my answer that will makes you to know about cusror. In mongoDb cursor will open for a operation and after that operation completion it will close immediately – Selva Kumar Sep 27 '16 at 08:00