Speed up a MongoDB find() with better sorting? (using mongoose orm)

时间:2021-01-05 08:43:39

Right now I have something like this....

现在我有这样的东西....

Item.find({}, function (docs) {
    for (var i = docs.length-15; i < docs.length; i++){
     client.send(JSON.stringify(docs[i]));
    }                   
});

but it seems to be very slow. I'm hoping to speed it up by doing something like...

但它似乎很慢。我希望通过做类似的事情加快速度。

Item.find().sort({_id:-1}).limit(15)...?

Is this possible? Will it help?

这可能吗?会有帮助吗?

Thanks!

2 个解决方案

#1


1  

From what I can gleam from the actual source code and tests, since mongoose 1.0.14 the sort() parameter has changed slightly to no longer accept an array. Furthermore, you seem to need to call find() again on the actual Query object which is returned on the find call (plus watching out for your err object). So:-

从我从实际的源代码和测试中可以看出,从mongoose 1.0.14开始,sort()参数略有改变,不再接受数组。此外,您似乎需要再次在查询调用上返回的实际Query对象上调用find()(另外还要注意您的错误对象)。所以:-

  Item.find().sort('_id','descending').limit(15).find(function(err, doc) {
    client.send(JSON.stringify(doc));
  });

Hopefully might do what you need.

希望可以做你需要的。

#2


3  

If you are only interested in the first (or last as in the case of sorting with _id: -1) 15 documents then yes, setting a limit on the query is a very good idea. Limiting on the client side as in your first example means that the database sends every single document to the client, and then the client ignores every but the last 15.

如果您只对第一个(或使用_id:-1排序的情况下的最后一个)15个文档感兴趣,那么设置查询限制是一个非常好的主意。在第一个示例中限制客户端意味着数据库将每个文档发送到客户端,然后客户端忽略除最后15个之外的所有文档。

However, the Mongoose syntax for specifying a limit is different from the Mongo shell syntax, here what I think you want:

但是,用于指定限制的Mongoose语法与Mongo shell语法不同,这里我想你想要的:

Item.find().sort([['_id','descending']]).limit(15).each(function(doc) {
  client.send(JSON.stringify(doc));
});

If I'm not mistaken you can chain a number of actions on a Mongoose query, and then call each to send it and get each document of the result passed to your callback.

如果我没弄错的话你可以在Mongoose查询上链接一些动作,然后调用每个动作发送它并将结果的每个文档传递给你的回调。

#1


1  

From what I can gleam from the actual source code and tests, since mongoose 1.0.14 the sort() parameter has changed slightly to no longer accept an array. Furthermore, you seem to need to call find() again on the actual Query object which is returned on the find call (plus watching out for your err object). So:-

从我从实际的源代码和测试中可以看出,从mongoose 1.0.14开始,sort()参数略有改变,不再接受数组。此外,您似乎需要再次在查询调用上返回的实际Query对象上调用find()(另外还要注意您的错误对象)。所以:-

  Item.find().sort('_id','descending').limit(15).find(function(err, doc) {
    client.send(JSON.stringify(doc));
  });

Hopefully might do what you need.

希望可以做你需要的。

#2


3  

If you are only interested in the first (or last as in the case of sorting with _id: -1) 15 documents then yes, setting a limit on the query is a very good idea. Limiting on the client side as in your first example means that the database sends every single document to the client, and then the client ignores every but the last 15.

如果您只对第一个(或使用_id:-1排序的情况下的最后一个)15个文档感兴趣,那么设置查询限制是一个非常好的主意。在第一个示例中限制客户端意味着数据库将每个文档发送到客户端,然后客户端忽略除最后15个之外的所有文档。

However, the Mongoose syntax for specifying a limit is different from the Mongo shell syntax, here what I think you want:

但是,用于指定限制的Mongoose语法与Mongo shell语法不同,这里我想你想要的:

Item.find().sort([['_id','descending']]).limit(15).each(function(doc) {
  client.send(JSON.stringify(doc));
});

If I'm not mistaken you can chain a number of actions on a Mongoose query, and then call each to send it and get each document of the result passed to your callback.

如果我没弄错的话你可以在Mongoose查询上链接一些动作,然后调用每个动作发送它并将结果的每个文档传递给你的回调。