小编Abh*_*jit的帖子

Fetch immediate next and previous documents based on conditions in MongoDB

Background

I have the following collection:

article {
  title: String,
  slug: String,
  published_at: Date,
  ...
}
Run Code Online (Sandbox Code Playgroud)

MongoDB version: 4.4.10

The problem

Given an article, I want to fetch the immediate next and previous articles depending on the published_at field of that article.

假设我有一篇带有published_atas的文章100。有很多文章的内容published_at小于100,也有很多文章的内容published_at大于100。我希望管道/查询仅获取具有或或最接近可能published_at值的文章。99101

尝试

这是我的聚合管道:

const article = await db.article.findOne({ ... });

const nextAndPrev = db.article.aggregate([
    {
        $match: {
            $or: [
                {
                    published_at: { …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
1399
查看次数