简体   繁体   English

MongoDB (PyMongo) 分页不同,结果不一致

[英]MongoDB (PyMongo) Pagination with distinct not giving consistent result

I am trying to achieve pagination with distinct using pymongo.我正在尝试使用 pymongo 实现不同的分页。

I have records我有记录

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

I only want name for each record, where name can be duplicate, so i want distinct name with pagination.我只想要每条记录的名称,其中名称可以重复,所以我想要带有分页的不同名称。

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

Problem is, with this query, each time I query I get different result for same page number问题是,对于这个查询,每次我查询时,我都会得到相同页码的不同结果

Looked into this answer看了这个答案

Distinct() command used with skip() and limit() Distinct() 命令与 skip() 和 limit() 一起使用

but didn't help in my case.但在我的情况下没有帮助。

How do I resolve this.我该如何解决这个问题。

Thanks in advance!提前致谢!

I've tried to sort after the group and it seems to solve the problem我试过对组进行排序,似乎解决了问题

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

try it here在这里试试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM