不能在Nodejs中使用Mongodb

当我尝试从集合中的字段(“acc”)获取所有不同的值时,我遇到了一个问题

var mongojs = require('mongojs'); var mongodb = mongojs('mongodb://localhost:27017/na'); var mongodb_data = mongodb.collection('data'); mongodb_data.distinct( "acc", (function(err, docs){ if(err){ return console.log(err); } if(docs){ console.log(docs); } }) ); 

当我运行代码时,它给出了下面的错误跟踪

 (node:45480) DeprecationWarning: sys is deprecated. Use util instead. /.../node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^ TypeError: cb is not a function at /U.../node_modules/mongojs/lib/collection.js:69:5 at handleCallback (/.../node_modules/mongodb/lib/utils.js:95:56) at /.../node_modules/mongodb/lib/db.js:313:5 at /.../node_modules/mongodb-core/lib/connection/pool.js:455:18 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9) 

distinct方法接受一个query对象作为第二个参数,试试这个:

 mongodb_data.distinct( "acc", {}, // query object (function(err, docs){ if(err){ return console.log(err); } if(docs){ console.log(docs); } }) );