简体   繁体   English

如何优化列表所有mongoose查询?

[英]How to optimize a list all mongoose query?

In the last few days I was busy trying to improve & optimize as mush as I can the mongoose queries for a website. 在过去的几天里,我一直在努力改进和优化,因为我可以对网站进行mongoose查询。 The main thing I found for that was to use indexes which I've already done. 我发现的主要是使用我已经完成的索引。 But there's a problem... In my mongo schemas I have few "list all" queries , what I mean by that is : A query that will return everything from that model and it doesn't have any parameter . 但是有一个问题......在我的mongo模式中,我几乎没有“列出所有”查询,我的意思是:一个查询将返回该模型中的所有内容,并且它没有任何参数。

I'm currently working on NodeJS v8.15.1 with ExpressJS and MongoDB v4.0.6 我目前正在使用ExpressJS和MongoDB v4.0.6开发NodeJS v8.15.1

The kind of query I'm talking about , and the example below is one of my actually queries : 我正在谈论的那种查询,以下示例是我的实际查询之一:

ModelSchema.statics.listAll = function ( callback ){
 try {
  return  this.find( {}, callback ).select({"x":0,"y":0});
 } catch (err) {
  console.log(err);
  return null;
 }
};

As you can see , I'm trying to get everything from that model excepting the x & y fields. 正如你所看到的,我正试图从x&y字段以外的那个模型中获取所有内容。

The query is perfectly working , there isn't any problem. 查询完全正常,没有任何问题。 As long as it doesn't use any indexed field it won't run faster or anything like that. 只要它不使用任何索引字段,它就不会运行得更快或类似。 But I wonder if there is a way to optimize it ? 但我想知道是否有办法优化它?

Thank you very much for help! 非常感谢您的帮助!

我想我已经找到了答案:我可以通过缓存请求来解决这个问题,并在每次更改时再次查询它。

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

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