Reputation: 10466
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:
Answers:
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
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?
Upvotes: 2
Views: 3519
Reputation: 151072
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.
Upvotes: 2