MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

Node.js MongoDB संदर्भ का पुन:उपयोग करता है

mydb.js :

var mongodb= require('mongodb'),
  server = new mongodb.Server('staff.mongohq.com', 10030, {
    auto_reconnect: true
  }),
  db1 = new mongodb.Db('mydb', server);


// callback: (err, db)
function openDatabase(callback) {
  db1.open(function(err, db) {
    if (err)
      return callback(err);

    console.log('Database connected');

    return callback(null, db);
  });
}

// callback: (err, collection)
function authenticate(db, username, password, callback) {
  db.authenticate(username, password, function(err, result) {
    if (err) {
      return callback (err);
    }
    if (result) {
      var collection = new mongodb.Collection(db, 'test');

      // always, ALWAYS return the error object as the first argument of a callback
      return callback(null, collection);
    } else {
      return callback (new Error('authentication failed'));
    }
  });
}

exports.openDatabase = openDatabase;
exports.authenticate = authenticate;

use.js :

var mydb = require('./mydb');
// open the database once
mydb.openDatabase(function(err, db) {
  if (err) {
    console.log('ERROR CONNECTING TO DATABASE');
    console.log(err);
    process.exit(1);
  }

  // authenticate once after you opened the database. What's the point of 
  // authenticating on-demand (for each query)?
  mydb.authenticate(db, 'usernsame', 'password', function(err, collection) {
    if (err) {
      console.log('ERROR AUTHENTICATING');
      console.log(err);
      process.exit(1);
    }

    // use the returned collection as many times as you like INSIDE THE CALLBACK
    collection.find({}, {limit: 10})
    .toArray(function(err, docs) {
      console.log('\n------ 1 ------');
      console.log(docs);
    });

    collection.find({}, {limit: 10})
    .toArray(function(err, docs) {
      console.log('\n------ 2 ------');
      console.log(docs);
    });
  });
});

परिणाम:

सफलता पर:

विफलता पर:

[मूल उत्तर]:

आप db खोल रहे हैं कई बार (प्रत्येक query . में एक बार ) आपको डेटाबेस को केवल एक बार खोलना चाहिए, और db . का उपयोग करना चाहिए बाद में उपयोग के लिए कॉलबैक में ऑब्जेक्ट करें।

आप एक ही चर नाम का कई बार उपयोग कर रहे हैं, और इससे कुछ भ्रम हो सकता है।

var mongodb = require('mongodb'),
    server = new mongodb.Server('staff.mongohq.com', 10030, {
        auto_reconnect: true
    }),
    db1 = new mongodb.Db('mydb', server);

function authenticateAndGo(db, handle) {
    db.authenticate('username', 'password', function(err) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('Database user authenticated');

        var collection = new mongodb.Collection(db, 'test');

        handle(collection);
    });
}

function query(handle) {
    db1.open(function(err, db2) {
        if( err ) {
            console.log(err);
            return;
        }
        console.log('Database connected');

        authenticateAndGo(db2, handle);
    });
};
exports.query = query;

मैंने उपरोक्त कोड को थोड़ा बदल दिया है (db1 मूल डीबी के लिए, db2 खोले . के लिए डीबी)। जैसा कि आप देख सकते हैं, आप db1 खोल रहे हैं कई बार, जो अच्छा नहीं है। किसी अन्य विधि में खोलने के लिए कोड निकालें और इसे एक बार उपयोग करें और db2 . का उपयोग करें आपके सभी प्रश्नों/अपडेट/निकालने/...

. के लिए उदाहरण

  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. मोंगो शैल में क्वेरी सिंटैक्स त्रुटि देता है:अनुपलब्ध:संपत्ति के बाद

  2. मोंगोडब कुल कमांड/पाइपलाइन में किसी प्रकार की पंक्ति संख्या जोड़ें

  3. नेवला खोजएक छँटाई के साथ

  4. $ और/$ या के आधार पर फ़िल्टर करने के लिए MongoDB के लिए एकत्रीकरण का उपयोग कैसे करें?

  5. यह एसिंक्स फ़ंक्शन के बाहर प्रतीक्षा क्यों करता है?