简体   繁体   English

猫鼬模型将多个功能应用于模型对象

[英]Mongoose Model applying multiple functions to a Model Object

I'm usting Nodejs Expressjs MongoDB and Mongoose to create rest API for a small service-app I work on. 我正在使用Nodejs Expressjs MongoDB和Mongoose为我正在使用的小型服务应用程序创建rest API。

I did all routes applying single straightforward functions like .find() .findOneAndUpdate() and etc. Like this one: 我使用所有简单的简单函数(例如.find().findOneAndUpdate()等)进行了所有路由。像这样:

    router.get('/testTable', function(req, res, next) {
    TestModel.find(function (err, testTableEntries) {
        if (err) return next(err);
        res.send(testTableEntries);
    });
});

Pretty simple. 很简单 But, what if I want to incorporate more functions then just single mongo function .find() 但是,如果我想合并更多函数,然后仅合并一个mongo函数.find()怎么办?

What if I want to: 如果我想:

 .find().pretty()

Or if want to aggregate, make some counts: 或者,如果要汇总,请计算以下几点:

.find().count()
.find({"fieldName": "How many have this value?"}).count()
.find({"fieldName": "How many have this value?"}).count().pretty()

I tried something like: 我尝试了类似的东西:

    router.get('/testTable', function(req, res, next) {
    TestModel.find(function (err, testTableEntries) {
        if (err) return next(err);
        res.send(testTableEntries);
    }).count();
});

Or maybe you can advice callbackless solution with promise (like Bluebird), my first thought is: 或者,也许您可​​以建议带有承诺的无回调解决方案(例如Bluebird),我的第一个想法是:

router.get('/testTable', function(req, res, next) {
    TestModel.find(function (err, next) {
        if (err) return next(err);
    }).then(function (req, res, next) {
        res.send(testTableEntries);
    }).catch(function (err, next) {
        if (err) return next(err);
    });
});    

Maybe there's some Mongoose built-in functions that can solve it, I'll be grateful, but also would be useful to know how to call functions in chain one after another on Mongoose Models. 我会很感激,也许有一些Mongoose内置函数可以解决它,但是,如果知道如何在Mongoose模型上一个接一个地调用函数,这将非常有用。

I'm grateful in advance for any advises and thoughts! 提前感谢您的任何建议和想法!

Well you can always just call .count() directly: 好吧,您始终可以直接直接调用.count()

Test.count({"fieldName": "How many have this value?"},function(err,count) { 
   // count is the counted results
});

And of course the object returned from `.find() in mongoose is an array, so: 当然,猫鼬从`.find()返回的对象是一个数组,因此:

Test.find({"fieldName": "How many have this value?"},function(err,results) {
    var count = results.length;
})

Or for "chaining" you can do this with "query" .count() 或者对于“链接”,您可以使用“ query” .count()

Test.find({"fieldName": "How many have this value?"})
   .count().exec(function(err,results) {
})

And if you want the "counts" of all possible "fieldName" values then use .aggregate() : 而且,如果您想对所有可能的“ fieldName”值进行“计数”,请使用.aggregate()

Test.aggregate([
    { "$group": {
        "_id": "fieldName",
        "count": { "$sum": 1 }
    }},
],function(err,results) {
   // each result has a count
});

You generally need to start thinking in terms of "working inside" the callback in async programming and not "returning" results from your method call to an "outside" variable. 通常,您需要开始考虑在异步编程中“内部处理”回调,而不是将方法调用的结果“返回”给“外部”变量。

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

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