MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

di nodejs, cara menghentikan perulangan FOR hingga panggilan mongodb kembali

"async " adalah modul yang sangat populer untuk mengabstraksi perulangan asinkron dan membuat kode Anda lebih mudah dibaca/dipelihara. Misalnya:

var async = require('async');

function getHonorStudentsFrom(stuObjList, callback) {

    var honorStudents = [];

    // The 'async.forEach()' function will call 'iteratorFcn' for each element in
    // stuObjList, passing a student object as the first param and a callback
    // function as the second param. Run the callback to indicate that you're
    // done working with the current student object. Anything you pass to done()
    // is interpreted as an error. In that scenario, the iterating will stop and
    // the error will be passed to the 'doneIteratingFcn' function defined below.
    var iteratorFcn = function(stuObj, done) {

        // If the current student object doesn't have the 'honor_student' property
        // then move on to the next iteration.
        if( !stuObj.honor_student ) {
            done();
            return; // The return statement ensures that no further code in this
                    // function is executed after the call to done(). This allows
                    // us to avoid writing an 'else' block.
        }

        db.collection("students").findOne({'_id' : stuObj._id}, function(err, honorStudent)
        {
            if(err) {
                done(err);
                return;
            }

            honorStudents.push(honorStudent);
            done();
            return;
        });
    };

    var doneIteratingFcn = function(err) {
        // In your 'callback' implementation, check to see if err is null/undefined
        // to know if something went wrong.
        callback(err, honorStudents);
    };

    // iteratorFcn will be called for each element in stuObjList.
    async.forEach(stuObjList, iteratorFcn, doneIteratingFcn);
}

Jadi Anda bisa menggunakannya seperti ini:

getHonorStudentsFrom(studentObjs, function(err, honorStudents) {
    if(err) {
      // Handle the error
      return;
    }

    // Do something with honroStudents
});

Perhatikan bahwa .forEach() akan memanggil fungsi iterator Anda untuk setiap elemen dalam stuObjList "secara paralel" (yaitu, tidak akan menunggu satu fungsi iterator selesai dipanggil untuk satu elemen array sebelum memanggilnya pada elemen array berikutnya). Ini berarti bahwa Anda tidak dapat benar-benar memprediksi urutan fungsi iterator--atau yang lebih penting, panggilan database--akan berjalan. Hasil akhirnya:urutan siswa berprestasi yang tidak terduga. Jika urutannya penting, gunakan .forEachSeries() fungsi.



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Tentukan Beberapa Kriteria untuk Elemen Array

  2. Otentikasi Powershell Mongodb

  3. Mongoose find(), bagaimana cara mengakses dokumen hasil?

  4. Skrip shell MongoDB menggunakan proyeksi untuk memformat tanggal dan mendapatkan waktu lokal

  5. Hasil grup dengan interval waktu 15 menit di MongoDb