mongodb nodejs每个vs toArray [英] mongodb nodejs each vs toArray

查看:118
本文介绍了mongodb nodejs每个vs toArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经快速浏览了一下,没有发现任何让我满意的答案,但基本上我已经开始用express和mongodb使用node.js创建一个webapi,而不是通常的.Net MVC Web API路由



我注意到的一件事是为了返回一组结果,我以一个相当笨重的方式来做,或者至少感觉如何。

  app.get('/ property',function(req,res){
var propArray = [] ;
MongoClient.connect(settings.connection,
函数(err,db){
if(err)throw err;

var properties = db.collection( properties);

var searchParams = {
活动:true,
已删除:false
}

properties.count(searchParams ,function(err,count){
properties.find(searchParams).toArray(function(err,result){
for(i = 0; i<计数; i ++)
propArray.push(new models.propertyModel(result [i]));

db.close();

return res.json(propArray);
});
});
}
);
});

现在我注意到有一个 .each 函数而不是 .toArray ,我喜欢使用,因为我可以删除 .count 函数,但显然您只能返回一次响应。我想知道你们能否启发我的一些你的知识。

  properties.find(searchParams).each(function err,result){
return res.json(result);
});

这样的东西,切出6行代码和额外的数据库调用。 >

解决方案

count()仍然可以用toArray():

  properties.find(searchParams).toArray(function(err,result){
var i,count;
for(i = 0,count = result.length ; i< count; i ++){
propArray.push(new models.propertyModel(result [i]));
}
db.close();
return res .json(propArray);
});


I've had a quick look around and not found anything that's satisfied me with an answer but basically I've started to use node.js with express and mongodb to create a webapi rather than the usual .Net MVC Web API route.

One thing I've noted though is in order to return a collection of results I'm doing it in a rather bulky way, or that's how it feels at least.

app.get('/property', function (req, res) {
    var propArray = [];
    MongoClient.connect(settings.connection,
        function (err, db) {
            if (err) throw err;

            var properties = db.collection("PROPERTIES");

            var searchParams = {
                Active: true,
                Deleted: false
            }

            properties.count(searchParams, function (err, count) {
                properties.find(searchParams).toArray(function (err, result) {
                    for (i = 0; i < count; i++)
                        propArray.push(new models.propertyModel(result[i]));

                    db.close();

                    return res.json(propArray);
                });
            });
        }
    );
});

Now I've noted that there's a .each function rather than .toArray which I would prefer to use as I could cut out the .count function but obviously you can only return a response once. I wondered if you guys could enlighten me with some of your mongo knowledge.

properties.find(searchParams).each(function (err, result) {
    return res.json(result);
});

Something like that, cutting out 6 lines of code and an extra call to the database.

解决方案

The count() can still be cut out with toArray():

   properties.find(searchParams).toArray(function (err, result) {
     var i, count;
     for (i = 0, count = result.length; i < count; i++) {
       propArray.push(new models.propertyModel(result[i]));
     }
     db.close();
     return res.json(propArray);
   });

这篇关于mongodb nodejs每个vs toArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆