0

我注意到我的 API 很慢。在城镇搜索时返回餐馆平均大约需要 4 秒(即使只有 10 个结果)。同样的搜索(没有 RegExp),直接在 Mongo Atlas 上是十分之一秒。这是我的 JavaScript 代码。我在我的 js 中使用 Express、Mongoose 和 RegExp。请问有什么办法可以加快速度吗?:

// Route to return all restaurants matching the searched name, returning all fields, using a collection that has collation//
app.route("/restaurants/:restaurantName")

  .get(function(req, res) {
    let partialToMatch = new RegExp(req.params.restaurantName, 'i');
    Restaurant.find({
      BusinessName: partialToMatch
    }, null, {
      limit: 25
    }, function(err, foundRestaurant) {
      if (foundRestaurant) {
        res.send(foundRestaurant)
      } else {
        res.send("No restaurants matching that title was found.");
      }
    });
  });
4

0 回答 0