不能在 Nodejs 中使用不同的 Mongodb [英] Cannot use Mongodb distinct in Nodejs
本文介绍了不能在 Nodejs 中使用不同的 Mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试从集合中的字段(acc")中获取所有不同的值时遇到问题
I am having a problem when I am trying to get all distinct values from a field("acc") in a collection
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);
}
})
);
当我运行代码时,它给出了下面的错误跟踪
When I run the code, it gave the error traces below
(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
对象作为第二个参数,试试这个:
distinct
method accept a query
object as the second param, try this:
mongodb_data.distinct(
"acc",
{}, // query object
(function(err, docs){
if(err){
return console.log(err);
}
if(docs){
console.log(docs);
}
})
);
这篇关于不能在 Nodejs 中使用不同的 Mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文