2

I'm new with mongodb and I was trying sort option by refering : http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#sorting

My code:

MongoClient.connect('mongodb://127.0.0.1/test', function (err, db) { //connect to mongodb   
    var collection = db.collection('qr');
    var options = {
        "limit": 20,
        "sort": "CARDNO"
    }
    collection.find({}, options).toArray(function (err, data) {
        console.log(data.length);
        res.send(JSON.stringify(data));
    });
    console.log("successfully connected to the database");
    //db.close();
});

Returns first 20 records of sorted result.

But when I remove the "limit"

MongoClient.connect('mongodb://127.0.0.1/test', function (err, db) { //connect to mongodb   
    var collection = db.collection('qr');
    var options = {
        "sort": "CARDNO"
    }
    collection.find({}, options).toArray(function (err, data) {
        console.log(data.length);
        res.send(JSON.stringify(data));
    });
    console.log("successfully connected to the database");
    //db.close();
});

I'm getting error

I tried

console.log(err)

It's giving [Error: parseError occured] in the console

/home/local/user/Desktop/node10k/routes/user.js:23
                console.log(data.length);
                                ^
TypeError: Cannot read property 'length' of null

Note: My collection contains 84k records.

My issue: It's working fine if I put some limit, I tried up to 64k limit its working when I try 74k I'm getting issue [Error: parseError occured]

Result for "limit" : 64020

Express server listening on port 3000
successfully connected to the database
64020
GET /users 200 11735ms - 15.32mb

Result for "limit" : 74020

Express server listening on port 3000
successfully connected to the database
[Error: parseError occured]

Why am I getting error when "limit" is beyond 64k?

Neil notified me in a comment:

  1. Code had syntax issue
  2. It may be cause of issue with toArray

Answers:

  1. I got the code referring the mongodb page(Link given at the starting) So this is not a syntax issue its working fine with my code if limit is give. So not a Syntax error

  2. I tried

    collection.find().toArray(function (err, data) { if (err) { console.log(err); } else { console.log(data.length) } res.send(JSON.stringify(data)); });

And I'm getting result with no error.

Express server listening on port 3000
successfully connected to the database
84050
GET /users 200 17525ms - 20.1mb

So not a toArray() issue

This issue only comes with sort and the code is working fine with 64k Data. Why?

17
  • why didn't you try debugging if mongo find throws an error? What other debugging steps did you try? Commented Feb 21, 2014 at 6:17
  • @MukeshSoni I tried console.log(JSON.stringify(err)) its giving '{}' in the console Commented Feb 21, 2014 at 6:20
  • The first thing you should do is check if find method throws an error and print it - if(err) console.log(err); Commented Feb 21, 2014 at 6:20
  • 2nd, you can try the exact same query on mongo shell (with the debug mode) and check if there is a problem with it. Commented Feb 21, 2014 at 6:23
  • 1
    try adding an index on CARDNO. It might be an 'out of memory' issue. Let me try replicating it. Commented Feb 21, 2014 at 8:16

1 Answer 1

2

Your sort was never working in the first example. The syntax is wrong

var options = {
   "sort": { "CARNO": 1 }
};

Or -1 for descending.

Also, not the problem but I have seen you do the same on another question, you don't json.stringify the err, just log it to console. And then you would have seen what you have done wrong.

The following usage works just fine, so if you are not getting a result then you are doing something different to how it is presented here:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://127.0.0.1/test', function (err, db) {

  var collection = db.collection('stuff');

  var options = {
    "sort": { "duration": -1 },
  };
  collection.find({},options).toArray(function(err, data) {
    if (err) {
      console.log(err);
    }
    console.log(data);
  });

});

You can also chain the .sort() to the find before .toArray() if this still eludes you.

Sign up to request clarification or add additional context in comments.

12 Comments

Still getting the same, console.log(JSON.stringify(err)) results [Error: parseError occured]
@SreekeshOkky I see you're still not reading what we write. Read and compare to your code. You are doing something differently.
I think you are not getting my issue. It's working fine if I put some limit, I tried upto 64k limit its working when I try 74k I'm getting issue '[Error: parseError occured]'
@SreekeshOkky That's not a problem with sort. But rather with toArray. You should not change the goalposts on your question. You got answers and help, now you have a different problem.
@SreekeshOkky And as I stated at the beginning of the anwser, you query with the limit may have been "limiting" but it was not "sorting". You syntax was wrong. Try with the correct syntax and you will see different output when you use limit. Your problem is now not what you thought it was and is a different question. The question of "sort" not working is answered and closed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.