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

एक्सप्रेस और मोंगोडीबी के साथ एकाधिक, अलग, संग्रह से प्राप्त करें

async का उपयोग करें पुस्तकालय जो इस परिदृश्य के लिए सबसे उपयुक्त है। जहां आपको ऐसे कई कार्य चलाने की आवश्यकता होती है जो एक दूसरे पर निर्भर नहीं होते हैं और जब वे सभी समाप्त हो जाते हैं तो कुछ और करते हैं, आपको async.parallel() तरीका। हस्ताक्षर है async.parallel(tasks, callback) , जहां कार्य कार्यों की एक सरणी है।

यह तुरंत सभी कार्यों को समानांतर में चलाएगा, उन सभी के लिए अपने कार्य कॉलबैक को कॉल करने की प्रतीक्षा करें, और अंत में जब सभी कार्य पूरे हो जाएंगे तो यह कॉलबैक (अंतिम कॉलबैक) चलाएगा।

निम्नलिखित उदाहरण दर्शाता है कि इसे आपके उपयोग के मामले के लिए कैसे अनुकूलित किया जा सकता है:

router.get('/profile', function(req, res, next) {
    mongo.connect(url, function(err, db) {
        var locals = {};
        var tasks = [
            // Load users
            function(callback) {
                db.collection('users').find({}).toArray(function(err, users) {
                    if (err) return callback(err);
                    locals.users = users;
                    callback();
                });
            },
            // Load colors
            function(callback) {
                db.collection('colors').find({}).toArray(function(err, colors) {
                    if (err) return callback(err);
                    locals.colors = colors;
                    callback();
                });
            }
        ];

        async.parallel(tasks, function(err) { //This function gets called after the two tasks have called their "task callbacks"
            if (err) return next(err); //If an error occurred, let express handle it by calling the `next` function
            // Here `locals` will be an object with `users` and `colors` keys
            // Example: `locals = {users: [...], colors: [...]}`
            db.close();
            res.render('profile/index', locals);
        });
    });
});


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. मोंगोडब क्या किसी वस्तु को एकत्र करना संभव है?

  2. सेलजेएस v0.10 का उपयोग करके मोंगोडब से कैसे जुड़ें?

  3. नेवला के साथ आईडी द्वारा एक मोंगो उप-दस्तावेज़ का चयन कैसे करें?

  4. दो मानों के आधार पर तत्व खोजें

  5. node.js mongodb - collection.find ()। toArray (कॉलबैक) - कॉलबैक कॉल नहीं किया जाता है