0

So I have the following documents:

{
    _id:596fc7d71aa95179315b27ea
    check:1
    pro:1
    slug:"ediloc.com marinescu"
}

{
    _id:596fd4661aa9517c7709475e
    check:0
    pro:1
    slug:"ediloc.com2"
}

{
    _id:5971ea3d1aa9515dcfee30ed
    check:0
    pro:0
    slug:"asirom"
}

{
    _id:5971f8871aa9516112e2e997
    slug:"aesirom2"
    check:0
    pro:0
}

{
    _id:597b9d741aa951452b8e6028
    user_id:596fc73b1aa9517908b8a2f9
    slug:"bridgeway"
    check:0
    pro:0
}

{
    _id:597bb6d11aa951452b8e6029
    slug:"rca"
    check:1
    pro:0
}

{
    _id:597bce141aa9515587b821ad
    slug:"anabella"
    check:0
    pro:0
}

{
    _id:597bceaa1aa9515587b821ae
    slug:"diana"
    check:0
    pro:0
}

{
    _id:597bcee31aa9515587b821af
    slug:"ananas"
    check:0
    pro:0
}

{
    _id:597bd2a31aa95156f22e24f6
    slug:"plaza mall"
    check:0
    pro:0
}

{
    _id:597c63161aa95171192c54fd
    slug:"ediloc.com23423fg"
    check:1
    pro:0
}

And this is my MongoDB query in GO Language, I am retrieving all the documents that contain the string "a" and I sort them by PRO and CHECK fields:

var business []bson.M

oe := bson.M{
    "$match": bson.M{"slug": bson.M{"$regex": "a"}},
}
oa := bson.M{
    "$project": bson.M {"pro": 1, "check": 1, "slug":1},
}
ol := bson.M{
    "$limit" :3,
}
os := bson.M{
    "$skip" :skips, //this is 0,3,6,9 etc depending on the page number
}
or := bson.M{
   "$sort": bson.D{
        bson.DocElem{Name: "pro", Value: -1},
        bson.DocElem{Name: "check", Value: -1},
        bson.DocElem{Name: "slug", Value: 1},
 },
}


pipe := c.Pipe([]bson.M{oe, or, oa, os, ol })
if err := pipe.All(&business); err != nil { 
    log.Printf(err.Error())
}

Now these are the results that I am getting back for each page.

Page 1:

{
    _id:596fc7d71aa95179315b27ea
    check:1
    pro:1
    slug:"ediloc.com marinescu"
}

{
    _id:597bb6d11aa951452b8e6029
    slug:"rca"
    check:1
    pro:0
}

{
    _id:5971ea3d1aa9515dcfee30ed
    check:0
    pro:0
    slug:"asirom"
}

Page 2:

{
    _id:597b9d741aa951452b8e6028
    user_id:596fc73b1aa9517908b8a2f9
    slug:"bridgeway"
    check:0
    pro:0
}

{
    _id:5971f8871aa9516112e2e997
    name:"aesirom2"
    check:0
    pro:0
}

{
    _id:597bce141aa9515587b821ad
    slug:"anabella"
    check:0
    pro:0
}

Page 3:

{
    _id:597bce141aa9515587b821ad
    slug:"anabella"
    check:0
    pro:0
}

{
    _id:5971f8871aa9516112e2e997
    name:"aesirom2"
    check:0
    pro:0
}

{
    _id:597bceaa1aa9515587b821ae
    slug:"diana"
    check:0
    pro:0
}

As you can see there are duplicated results on the second the third page.

Any thoughts how I can write this query so it doesn't retrieve duplicated documents when you go to a different page?

  • When "sorting" on a value that is not actually "unique" you need to keep track of something that "is unique" and then make sure you exclude those results by each "ranged" page. [All explained in the linked answer.](https://stackoverflow.com/questions/28105009/implementing-pagination-in-mongodb) – Neil Lunn Aug 01 '17 at 13:00
  • @NeilLunn I edited my query and added also `bson.DocElem{Name: "slug", Value: 1},` Now I see that I am getting no duplicates, is this correct approach? – user3863487 Aug 01 '17 at 13:11
  • How was ["read the linked duplicate question and answer"'](https://stackoverflow.com/questions/28105009/implementing-pagination-in-mongodb) an unclear statement here? Your question has been asked before. Your question has been solved before. You "exclude" the same sort values from each page using `$nin` and an list of "seen _id values". All **explained at length already**. – Neil Lunn Aug 01 '17 at 13:14

0 Answers0