【问题标题】:MongoDB (PyMongo) Pagination with distinct not giving consistent resultMongoDB(PyMongo)分页具有不同的不给出一致的结果
【发布时间】:2021-02-09 12:55:19
【问题描述】:

我正在尝试使用 pymongo 实现不同的分页。

我有记录

{
  name: string,
  roll: integer,
  address: string,
  .
  .
}

我只想要每条记录的名称,其中名称可以重复,所以我想要分页的不同名称。

result = collection.aggregate([
    {'$sort':{"name":1}},
    {'$group':{"_id":"$name"}},
    {'$skip':skip},
    {'$limit':limit}
])

问题是,对于这个查询,每次我查询相同的页码都会得到不同的结果

查看了这个答案

Distinct() command used with skip() and limit()

但对我没有帮助。

我该如何解决这个问题。

提前致谢!

【问题讨论】:

    标签: python mongodb nosql pymongo


    【解决方案1】:

    我试过按组排序,似乎解决了问题

    db.collection.aggregate([
      {
        "$group": {
          "_id": "$name"
        }
      },
      {
        "$sort": {
          "_id": 1
        }
      },
      {
        "$skip": 0
      },
      {
        "$limit": 1
      }
    ])
    

    试试here

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 2017-04-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2019-04-23
      相关资源
      最近更新 更多