使用mongoose查询大量数据的最佳和最快方法是什么?

时间:2015-09-05 17:46:43

标签: node.js performance mongoose

我想做以下事情:

我的数据库中有超过10,000个用户。

我需要向他们发送一个会改变文档中值的事件,

例如:

user:{
   money:10,
   skill : 5,
   mood  : 1
}

所以这就是我想要避免的,因为它是一个记忆和一个cpu地狱:

User.find({}).exec(function(err,users){
   users.forEach(function(user){
      if(user.money < 10){
         user.money += 5 * (some other params or something);
      }
      user.save();
   });
);

此外,我需要提取每个用户资金低于10的用户的ID并向他发送推送....所以我不能只使用&#34;更新&#34;,&#34; inc&#34;或&#34;设置&#34;

此代码崩溃了我的服务器,我怎样才能让它变得更好?我应该使用异步吗?如果有,怎么样?

2 个答案:

答案 0 :(得分:1)

您可以使用update运算符和{multi: true}选项使用一个User.update( {money: {$lt: 10}}, {$inc: {money: 5 * (some other params or something)}}, {multi: true}, function(err, num) { ... }); 来执行此操作,以便将其应用于所有匹配的文档:

var stream = User.find({}).stream();
stream.on('data', function(user) {
    stream.pause();
    if(user.money < 10){
       user.money += 5 * (some other params or something);
    }
    // More document-specific updates
    ...
    user.save(function(err, doc) {
        // The changes to this doc are complete, move on to the next one.
        stream.resume();
    });
}).on('error', function(err) {
    console.error(err);
}).on('close', function() {
    console.log('All done!');
});

如果您的更新是每个文档都需要根据其内容进行特殊处理,那么您可以使用$inc方法在任何时候限制内存中的文档数量:

<VirtualHost *:80>

ServerAdmin webmaster@localhost
DocumentRoot /home/cgtopher/public_html/

<Directory /home/cgtopher/public_html/>
Options FollowSymLinks Indexes
AllowOverride None
Order allow,deny
allow from all
</Directory>

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>

#cgi-bin

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory "/usr/lib/cgi-bin">

AllowOverride None

Options ExecCGI FollowSymLinks

Order allow,deny

Require all granted

</Directory>

ErrorLog /home/cgtopher/.apache/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

答案 1 :(得分:0)

Query Stream怎么样?在你的例子中,你将所有用户带入进程内存,然后进行操作,使用流你可以获得单个文档并处理它。

相关问题