0

I've a mongodb collection which has more than 20 million collection.

db.collection.find({ 'attr.type': new RegExp('automotive snow' 'i') }).limit(10);

Above mongo shell query returns the results as expected, But implementing the same in a node.js application it doesn't seem to work right. I can't really figure out the reason.

Sample node.js code

db.collection.find({ 'attr.type': new RegExp('automotive snow' 'i') }, function(err, docs) {
 console.log(docs); // returns undefined
});

I use mongojs module, Tried native mongodb module as well but the results seemed to be the same hence decided to use mongojs module.

Does anyone know what the issue is?

sachq
  • 672
  • 7
  • 11
  • 1
    If `docs` is `undefined`, `err` should hold an error. What does it say? – qqilihq Dec 25 '16 at 15:02
  • Thank you. That helped me understand a bit more about the problem. It shows `connection 1 to server.ip timed out`. – sachq Dec 26 '16 at 02:24
  • Fixed by setting SocketTimeOut options on mongo connection. – sachq Dec 26 '16 at 03:33
  • what happen if you use `db.collection("collectionName").find` ? – Shaishab Roy Dec 26 '16 at 05:03
  • @ShaishabRoy I was using a different module `mongojs` like i mentioned in the question. Right now i switched to `mongodb` and set a socket timeout to solve the issue. – sachq Dec 26 '16 at 05:47

0 Answers0