我在NodeJS和MongoDB中特别陌生。我有一种情况,我需要使用多个查询来获得结果。我不想运行每个查询,然后再运行。相反,我想批量收集(?)查询,然后一次执行所有查询。我正在尝试以下代码。
let bulk = Requests.collection.initializeUnorderedBulkOp();
let allTerms = await getAllTerms(market).catch(e => console.error("Error: ", e.message))
allTerms.forEach(term => {
console.log(term); // Getting printed fine
bulk.find({ 'term': term['name'], 'currency': market, 'status': 'Live' });
});
console.log("Now bulking"); // this one too, and sequentially
bulk.execute().then((result) => console.log(result)).catch((e) => "Error Occured!"); // But this one is throwing error UnhandledPromiseRejectionWarning: MongoError: Invalid Operation, no operations specified
console.log(":Operations:"); // Above errors being logged before this statement.
那是因为批量仅支持写入吗?如果是这样,哪种方法是只读执行的最佳选择?
P.S。我的猫鼬在使用mongoose.Promise = require('bluebird');