将MongoDB聚集器响应转换为500K+记录需要大量时间

发布于 2025-02-07 18:41:47 字数 727 浏览 1 评论 0原文

很抱歉提出重复的问题,但我提到了太多文章,但无法解决我的用例。

使用的技术

  1. nodejs
  2. mongodb(mongoclient)librabry用nodejs

我尝试了两种方法:

  const records = client
    .db(mongodbDATABASE)
    .collection('subscriptions')
    .aggregate(pipeline)
    .toArray()
  1.   const Records =客户端
      .db(mongodbdatabase)
      .Collection(“订阅”)
      .ggregate(管道)
    
    而(等待records.hasnext()){
      const uscription =等待records.next()
      //处理订阅
    }
     

我在Compass和NoSQL-Booster中运行聚合管道查询,并且我的响应非常快(200ms),使用500K记录,但是每当我尝试通过使用上面的汇总结果将汇总结果转换为阵列格式方法(.toArray(),.hasnext())几乎需要 3-4分钟

您能为此提供帮助,我如何将此聚合库转换为阵列格式,而不会影响性能?

Sorry to ask repeated questions, but I referred to too many articles but was not able to solve my use case.

Technology used

  1. NodeJs
  2. mongodb (MongoClient ) librabry with NodeJs

I have tried two approaches as below:

  const records = client
    .db(mongodbDATABASE)
    .collection('subscriptions')
    .aggregate(pipeline)
    .toArray()
  1. const records = client
      .db(mongodbDATABASE)
      .collection('subscriptions')
      .aggregate(pipeline)
    
    while (await records.hasNext()) {
      const subscription = await records.next()
      //TO DO with subscriptions
    }
    

I run aggregate pipeline query in compass and NoSQL-booster and I'm getting responses very quick (200ms) with 500K records but whenever I'm trying to convert the aggregate result to array format by using the above methods(.toArray(), .hasNext() ) it's almost taking 3-4 minutes.

Could you please help on it how I can convert this AggregationCursor to array format without much impacting the performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-02-14 18:41:47

光标模式不会一次获取所有数据,因此仅在200ms中返回。要获取500k行,无论您对客户端采用哪种方法,都需要一段时间,因为这将需要大量数据库服务器的工作。

the cursor pattern is not going to fetch all data at once, hence it is returning in only 200ms. to fetch 500k rows is going to take a while no matter what approach you take on the client, as this will require a lot of work for the database server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文