3

I am trying to search data in mongodb with nodejs. This is my query

collection.find({ age: { '$gt': 20 } });

its working fine in robomongo but giving me this response in my application

Readable {
 pool: null,
 server: null,
 disconnectHandler: 
  { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
   length: [Getter] },
  bson: {},
  ns: 'versioncontrol.Branch/contacts',
 cmd: 
  { find: 'versioncontrol.Branch/contacts',
    limit: 0,
 skip: 0,
  query: { age: [Object] },
 slaveOk: true,
 readPreference: { preference: 'primary', tags: undefined, options: [Object] } }

Now i don't know how to get my data out of it.

Abdul Bari
  • 149
  • 1
  • 2
  • 12

2 Answers2

1

The cursor returned from the find method is a Readable stream. You have do read items from it to get the actual result. Look at this

Example:

var cursor = collection.find({ age: { '$gt': 20 } });
cursor.each(function (err, doc) {
  if (err) {
    console.log(err);
  } else {
    console.log('Fetched:', doc);
  }
});
Erik
  • 2,888
  • 2
  • 18
  • 35
0

Done it using

var cursor = collection.find({ age: { '$gt': 20 } }).toArray();
cursor.then(function (docs) {
 console.log( docs ); 
});
Abdul Bari
  • 149
  • 1
  • 2
  • 12