简体   繁体   English

与数组不同的NodeJS Mongodb无法正常工作

[英]NodeJS Mongodb distinct to array does not work

Hello try to use an mongodb distinct query with NodeJS (Async). 您好,尝试将Mongodb独特查询与NodeJS(Async)结合使用。 In the GUI of Mongo this query works but in Node it returns the following error 在Mongo的GUI中,此查询有效,但在Node中,它返回以下错误

TypeError: db.collection(...).distinct(...).toArray is not a function

The error returns on the following statement: 该错误返回以下语句:

mongo.connect(uri, function (err, db) {
    console.info('MONGODB START CHECK COLLECTIONS')
    var tasks = [   // Load businessrules
        function (callback) {
            db.collection('businessrules').find({typeBusinessRule: 'SpiderGraphExeption'}).toArray(function (err, businessrules) {
                if (err) return callback(err);
                locals.businessrules = businessrules;
                callback();
            });
        },
        // Load stgOmniTracker
        function (callback) {
            db.collection('stgOmniTracker').find({}).toArray(function (err, tickets) {
                if (err) return callback(err);
                locals.tickets = tickets;
                callback();
            });
        },
        // HERE STARTS THE ERROR
        function (callback) {
            db.collection('stgOmniTracker').distinct("Responsible Group").toArray(function (err, group) {
                if (err) return callback(err);
                locals.group = group;
                callback();
            });
        }
    ];
    console.info('--------------- START ASYNC ------------------------')
    async.parallel(tasks, function (err) {
        if (err) return next(err);
        var businessrules = locals.businessrules, tickets = locals.tickets, resultSet = {}, aggCountsPerDayCattegory = [], group = locals.group
        db.close()
}

I hope you can help me to fix this. 希望您能帮助我解决此问题。 Manny Thanks 曼尼谢谢

Erik 埃里克

db.collection('stgOmniTracker').distinct("Responsible Group", function(err, result)
{
//your code here
});

In the mongodb docs you can see that distinct returns null . mongodb文档中,您可以看到distinct返回null

distinct(key[, query][, options], callback) 与众不同(键[,查询] [,选项],回调)

Arguments: 参数:

 key (string) – key to run distinct against. [query] (object) – option query to narrow the returned objects. [options] (object) – additional options during update. callback (function) – this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from distinct or null if an error occured. 

**Returns: ** **回报:**

null

It takes a callback as the last argument. 它以回调作为最后一个参数。 In this callback, the second argument contains the results you are looking for. 在此回调中,第二个参数包含您要查找的结果。

So your updated code would look like this: 因此,您更新的代码如下所示:

function (callback) {
  db.collection('stgOmniTracker')
    .distinct("Responsible Group", function (err, group) {
        if (err) return callback(err);
        locals.group = group;
        callback();
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM