1

ids = db.students.find({}, { _id: 1 });

It gives the length of ids as 2993 while students count is 2413. It's not storing the 20 documents which are being displayed on the shell.

2

The shell does not actually return just 20 records, what you get returned when you run a find like the one you list in the shell is a cursor, the shell then by default iterates over and displays the first 20 documents returned by that cursor (for convenience). For example:

> db.foo.find({}, {_id : 1})
{ "_id" : ObjectId("573c4afbaa6cd35206c16a8d") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a8e") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a8f") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a90") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a91") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a92") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a93") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a94") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a95") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a96") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a97") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a98") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a99") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9a") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9b") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9c") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9d") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9e") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16a9f") }
{ "_id" : ObjectId("573c4afbaa6cd35206c16aa0") }
Type "it" for more

Note the last line, if you type it you will get 20 more results, and you can repeatedly do so until you reach the end of the results and exhaust the cursor.

When you assign the result of a query to a variable you are storing the cursor in that variable, not the results, and no iteration is done unless you call it.

The reason for the differences in your count is not because of any skipping, it is probably simply because you have 2413 documents with a student field and 580 without a student field for a total of 2993. You haven't shared your actual doc structure, but to illustrate with a basic example:

// insert 2413 docs with a student field, 580 without
for(var i = 0; i < 2413; i++){db.foo.insert({student : i})};
for(var i = 0; i < 580; i++){db.foo.insert({})};
// now do a count of all records
db.foo.count()
2993
// count only those with student field
db.foo.count({student : {$exists : true}})
2413
1

Putting DBQuery.shellBatchSize = 0; in my js file solved the issue.

  • If this is the solution to your problem, you can mark this answer as your solution. – RLF May 27 '16 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.