数组上的MongoDB分页-检查$ slice是否到达数组的开头 [英] MongoDB pagination on arrays - Check if $slice reached the beginning of the array

查看:228
本文介绍了数组上的MongoDB分页-检查$ slice是否到达数组的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个框,用户可以在其中阅读评论。如果某些用户向下滚动,它将加载越来越多的注释,一切正常。我从头到尾对数组进行切片,因为每当有人发表评论时,我都会使用 $ push ,因此我将切片如下 $ slice:[- 5,5] 。这将从数组中切出最后5个元素。 -5 实际上是我的聚合管道中的变量。每当我加载一些注释时,我都会将值从 -5 减小到 -10 ,依此类推。我在这里遇到的问题是,如果我的数组的长度为50,例如,我执行了这个切片 $ slice:[-70,5] 我得到了前5条评论,但我希望收到0。其结果是,我在相同的5条第一条评论上有一个无限滚动条女巫。我该如何解决?

In my application i have an box where users can read comments. If some user scrolls down, it loads more and more comments, everything fine. I slice the array from the back to the begin because i use $push whenever somebody write some comment so i slice it like this $slice: [ -5 , 5]. This will slice the last 5 elements out of the array. The -5 is actually an variable in my aggregation pipeline. Whenever i loaded some comments i decrease the value from -5 to -10 and so on so on. The problem i got here is if my array has an legth for example of 50, and i execute this slice $slice: [ -70, 5] i get the first 5 comments, but i expected to receive 0. The result is that i have an infinity scrollbar witch loads over and over the same 5 first comments. How do i fix this?

let result = await Post.aggregate([
    {
      $match: {
        _id: userId,
        "posts._id": postId
      }
    },
    {
      $addFields: {
        posts: {
          $map: {
            input: "$posts",
            as: "p",
            in: {
              _id: "$$p._id",
              comments: { $slice: ["$$p.comments", -start, 5] }
            }
          }
        }
      }
    },
    {
      $project: {
        posts: {
          $filter: {
            input: "$posts",
            as: "posts",
            cond: { $eq: ["$$posts._id", postId] }
          }
        }
      }
    }
  ]);


推荐答案

@lfaruki,我认为您的做法正确反转数组,如文档所示,也由@whoami

@lfaruki, I think you are on the right track reversing the array, as from the documentation, as also explained by @whoami


< position> 如果为正,则 $ slice 确定从数组开头开始的位置。如果< position> 大于元素数,则 $ slice 返回一个空数组。

<position> If positive, $slice determines the starting position from the start of the array. If <position> is greater than the number of elements, the $slice returns an empty array.



comments: {
  $reverseArray: { // reverse back
    $slice: [
      {
        $reverseArray: "$comments"
      },
      endOffset, // this starts from 0, 5, 10, 15, ...
      5
    ]
  }
}

我不确定尝试使用反向数组实现它时遇到了什么问题。但是我的一般建议是,它将对大型阵列的性能产生负面影响。如果您已经在单独的集合中添加了注释,则最好使用基于时间 基于光标 的分页进行单独查询。因为最终MongoDB会限制文档的大小,并且您不想聚合处理大型数组。

I'm not sure what issues you faced when you try to implement it using the reverse array. But my general advice is it would have negative effect on performance on large arrays. If you already have your comments in a separate collection, better query it separately using "time-based" or "cursor-based" pagination. Because ultimately MongoDB has limits on document size and you don't want to process large arrays in aggregation.

这篇关于数组上的MongoDB分页-检查$ slice是否到达数组的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆