我需要借助异步库以下列方式迭代mongoshell中的数组:
load('/opt/kif/crons/dashboard/node_modules/async/lib/async.js');
async.series([
function(cb) { print("First"); cb(null,1); },
function(cb) { print("Second"); cb(null,2); },
function(cb) { print("Third"); cb(null,3); }
], function(err, res) {
printjson(res);
});
并正在运行
mongo testasync.js
返回
MongoDB shell version: 3.2.0
connecting to: test
First
2016-05-01T15:41:45.589+0000 E QUERY [thread1] ReferenceError: setTimeout is not defined :
_delay<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:196:9
async.eachOfSeries/iterate/<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:272:29
only_once/<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:44:13
_parallel/</<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:723:17
_restParam/<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:167:1
@testasync.js:3:33
_parallel/<@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:718:1
iterate@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:262:1
async.eachOfSeries@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:281:9
_parallel@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:717:1
async.series@/opt/kif/crons/dashboard/node_modules/async/lib/async.js:739:9
@testasync.js:2:1
failed to load: testasync.js
[root@NQPC002 dashboard]#
我该怎么做?
答案 0 :(得分:1)
虽然MongoDB shell使用JavaScript,但它的数据库访问方法都是同步的,因此已经连续执行了一系列调用。
因此像db.collection.update
这样的方法会直接返回WriteResult
而不是使用回调。