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

luwak :menemukan data dengan mengulang pada array model

Silakan periksa cuplikan kode ini, saya telah membuat contoh yang berfungsi dari apa yang Anda butuhkan.Silakan periksa komentar dalam kode untuk pemahaman yang lebih baik.

Contoh Kode kerja yang mirip dengan yang Anda butuhkan. Pertanyaan referensi lain untuk menggunakan async dengan luwak.

/*
 * Object to store all models
 */
var allRefDatasSchemas = {
  RefAllotement: mongoose.model('RefAllotement', RefDataSchema),
  RefModeleConstructeur: mongoose.model('RefModeleConstructeur', RefDataSchema),
  RefTypeKit: mongoose.model('RefTypeKit', RefDataSchema),
  RefTypeUtilisation: mongoose.model('RefTypeUtilisation', RefDataSchema),
};
/*
 * need an array to run all queries one by one in a definite order using async waterfall mwthod
 */
var arr = [];
for(each in allRefDatasSchemas) {
    arr.push(each);
}

/*
 * Callback function for initiation of waterfall
 */
var queue = [
    function(callback) {
        // pass the ref array and run first query by passing starting index - 0
        callback(null, arr, 0)
    }
];

/*
 * Object to store result of all queries
 */
var finalResult = {};

/*
 * Generic Callback function for every dynamic query
 */
var callbackFunc = function(prevModelData, currentIndex, callback) {
    allRefDatasSchemas[arr[currentIndex]].find(function(err, result) {
        if(err) {
            console.log(err)
        } else {

            // Your Query
            // 
            // I'd like to do something like that :
            // but this.modelName is null, because it isn't the model
            // on which the find is done.

            // arr[currentIndex] will point to 
            // RefAllotement, RefModeleConstructeur etc. as you required
            finalResult[arr[currentIndex]] = result

            // send current result to next interation if required or you can skip
            // and increment the currentIndex to call next query 
            callback(null, result, currentIndex + 1)
        }
    })
}

/*
 * Add callback function for every dynamic query
 */
for(each in allRefDatasSchemas) {
    queue.push(callbackFunc);
}

/*
 * Run all dynamic queries one by one using async.js waterfall method
 */
async.waterfall(queue, function (err, result) {
    // Final object with result of all the queries
    console.log('finish', finalResult)
});

keluaran akan dalam format ini

finish { RefAllotement:[
        // Result of RefAllotement query
    ],
    RefModeleConstructeur:[
        // Result of RefModeleConstructeur query
    ],
    RefTypeKit:[
        // Result of RefTypeKit query
    ],
  RefTypeUtilisation:[
        // Result of RefTypeUtilisation query
    ]
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Perbedaan antara menyimpan ObjectId dan bentuk stringnya, di MongoDB

  2. Berlangganan Koleksi Meteor.Users

  3. MongoDB $minggu

  4. Memformat Angka sebagai Persentase dalam SQL

  5. Luwak -- Paksa nama koleksi