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

कभी-कभी ब्राउज़र में डेटा प्रदर्शित नहीं किया जा रहा है

सबसे आसान लेकिन अनुशंसित नहीं आप जो चाहते हैं उसे करने का तरीका नीचे कोड होगा लेकिन यह आमतौर पर कॉलबैक नरक की ओर जाता है या कयामत का पिरामिड और इसे पढ़ना कठिन है इसलिए इसका उपयोग न करें !!!!

Comp.count({}, function(err, count){
   Comp.find({}).remove({}, function(){
      Comp.create(arr, function(err, docs){
         Comp.find({}, ..., function(err, doc){                
            Comp.findOne().skip(random).exec(function(err, result){
                res.render("index",{})
            })    
         }) 
      })
   })    
})

दूसरा तरीका async.js . का उपयोग करना हो सकता है

async.series([
    function(callback){
        Comp.count({}, function(err, count){
            callback(null, count);
        });
    },
    function(callback){
        Comp.find({}).remove({}, function(){
            callback(null);
        });
    },
    function(callback){
        Comp.create(arr, function(err, docs){
            callback(null);
        });
    },
    function(callback){
        Comp.find({}, ..., function(err, doc){ 
            callback(null);
        });
    },
    function(callback){
        Comp.findOne().skip(random).exec(function(err, lastResult){
            callback(null, lastResult);
        });
    }
],
// optional callback, results is an array of results from each callback if any
function(err, results){
    // results is now equal to [count, lastResult]
    res.render("index",{})
});

और अंत में वादे मैंने इसे आजमाया या उपयोग नहीं किया है, इसलिए 100% निश्चित नहीं है लेकिन कुछ ऐसा

var promise = Comp.count({}).exec();

promise.then(function(count) {
    return Comp.find({}).remove({}).exec();
})
.then(function() {
    return Comp.create(arr, ).remove({}).exec();
})
.then(function() {
    return Comp.find({}).remove({}).exec();
})
.then(function() {
    return Comp.find({}).skip(random).exec();
})
.then(function(result) {
    res.render("index",{})
})

नेवला पर वादों के बारे में कुछ और विवरणों के लिए यहां देखें कैसे नेवला का उपयोग करने के लिए वादा - mongo




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. पंक्ति पुनर्प्राप्ति के बिना MongoTemplate.aggregate निष्पादित करें

  2. मोंगो में अधिकतम () फ़ंक्शन नहीं है, मैं इसके आसपास कैसे काम करूं?

  3. सरणी में नेस्टेड दस्तावेज़ में फ़ील्ड के लिए अद्वितीय बाधा कैसे सेट करें?

  4. MongoDB में बेसिक क्वेरी ऑपरेशंस कैसे करें

  5. समग्र ढांचे के साथ एक नया सरणी फ़ील्ड कैसे बनाएं