0

I am wondering how I can make two successives or synhcronous access to two different collections in a MongoDB database. I need to take a parameter in a first collection to use it as a parameter of .find() method in the second query. Here is my code:

  MongoClient.connect(url, function(err, db) {
  db.collection('questions').find( { "status": "active" } ).toArray(
      function(err, item) {
        var fb_id = item[0]._id;
        console.log(fb_id);
        db.close();
        MongoClient.connect(url, function(err, db) {
          var cursorC = db.collection('comments').find({questionId : fb_id }).toArray(
            function(err, items) {
              console.log(items);
            }
          );
          db.close();
      });
    });
  });

I tried unsuccessfully to chain the two connection to the database, however the second query states an undefined result. When using both db.collection().find() function at the saeme level, in the MongoClient.connect() function I suppose both are executed asynchronously and it ends with another undefined result for the second function whose result depends on the first.

Do you have any idea to proceed using the MongoDB NodeJS driver I am using?

Thanks

AtoM_84
  • 145
  • 1
  • 3
  • 10
  • Are you closing and open the db connection on purpose? Try this: `MongoClient.connect(url, function(err, db) { db.collection('questions').find( { "status": "active" } ).toArray( function(err, item) { var fb_id = item[0]._id; console.log(fb_id); db.collection('comments').find({questionId : fb_id }).toArray( function(err, items) { console.log(items); } ); db.close(); }); });` – Bruno Apr 29 '16 at 19:38
  • Thank you for your comment. I tried this but this does not work, I think because the db object is no longer defined in the second function scope. I got an undefined result for items. So that is why I did two successive MongoDB connections and two db.close() call. – AtoM_84 Apr 29 '16 at 19:47
  • Do you have any error on the `err` variable? – Bruno Apr 29 '16 at 19:49
  • Using my code I have no error on the first db.collection query and this on the second: { [MongoError: server localhost:27017 sockets closed] name: 'MongoError', message: 'server localhost:27017 sockets closed' } . – AtoM_84 Apr 29 '16 at 19:54
  • have a look [here](http://stackoverflow.com/questions/14495975/why-is-it-recommended-not-to-close-a-mongodb-connection-anywhere-in-node-js-code/14607887#14607887). Basically, you want to open a connection only once and re-use it. Hope it helps – Bruno Apr 29 '16 at 19:57
  • Can you please show your database schema? I mean the objects in comments collection? – Sk Arif Apr 29 '16 at 20:17
  • Of course, here is an extract of them: { "_id" : ObjectId("5717c5ff8823052c2ec0b23e"), "questionId" : "5710cc8937e299941a0de3e7", "commentValue" : "rsgthfgjthkjh;hjkhj", "date" : 1461175807, "author" : "ddgddgddg", "like" : "0" } and some other document have no questionId field. – AtoM_84 Apr 29 '16 at 20:28

0 Answers0