go - mongoDB $sort inconsistent results -


i have following query in go lang works fine:

query["name"] = bson.m{"$regex": searchstr, "$options": "i"} query["likes"] = usersession.id c.find(query).skip(0).limit(2).select(bson.m{"name":1, "profile":1, "description":1, "user_id":1, "likes":1}).sort("-pro", "-check").all(&business); 

then tried write same query using aggregation framework:

query["name"] = bson.m{"$regex": searchstr, "$options": "i"} query["likes"] = usersession.id oe := bson.m{     "$match" :query, } oa := bson.m{     "$project": bson.m {"pro": 1, "check": 1, "name":1, "profile":1, "description":1, "user_id":1, "likes":1, "nrlikes": bson.m{ "$size": "$likes" }, "city": 1, "country": 1, "industry": 1}, } ol := bson.m{     "$limit" :pagesize, } os := bson.m{     "$skip" :skips, } or := bson.m{     "$sort" : bson.m {"pro": -1, "check": -1}, }  pipe := c.pipe([]bson.m{oe, oa, or, os, ol  })  pipe.all(&business) 

the second query works fine 90% of time, 10% of times returns different order results.

any thoughts?

later edit: here resuls

[]bson.m{ {     "description": "<p>sasdfdasf</p>",     "profile":     []interface {}{         "rkwmmxpwhegczwvgn2tzsru7jrorhorkwmmxpwhegczwvgn2tzsru7jrorho=0.jpg",     },     "likes": []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "nrlikes":  int(1),     "name":     "ediloc.com2",     "city":     "calimanesti",     "industry": "automotive",     "_id":      "yo\xd4f\x1a\xa9q|w\tg^",     "user_id":  "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "country":  "romania", }, {     "_id":         "yo\xc7\xd7\x1a\xa9qy1['\xea",     "user_id":     "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "name":        "ediloc.com",     "country":     "romania",     "description": "<p>a</p>",     "profile":     []interface {}{         "1sssysnrzwgjjwqzxghl6qzavfwzis1sssysnrzwgjjwqzxghl6qzavfwzis=1.jpg",     },     "likes": []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "nrlikes":  int(1),     "city":     "calimanesti",     "industry": "accounting", }, }   []bson.m{ {     "likes": []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "_id":     "yo\xd4f\x1a\xa9q|w\tg^",     "name":    "ediloc.com2",     "city":    "calimanesti",     "country": "romania",     "profile": []interface {}{         "rkwmmxpwhegczwvgn2tzsru7jrorhorkwmmxpwhegczwvgn2tzsru7jrorho=0.jpg",     },     "user_id":     "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "industry":    "automotive",,     "nrlikes":     int(1), }, {     "_id":      "yo\xc7\xd7\x1a\xa9qy1['\xea",     "user_id":  "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "industry": "accounting",     "profile":  []interface {}{         "1sssysnrzwgjjwqzxghl6qzavfwzis1sssysnrzwgjjwqzxghl6qzavfwzis=1.jpg",     },     "likes": []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "nrlikes":     int(1),     "name":        "ediloc.com",     "city":        "calimanesti",     "country":     "romania",     "description": "<p>a</p>", }, }   []bson.m{ {     "nrlikes":     int(1),     "user_id":     "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "description": "<p>a</p>",     "profile":     []interface {}{         "1sssysnrzwgjjwqzxghl6qzavfwzis1sssysnrzwgjjwqzxghl6qzavfwzis=1.jpg",     },     "country":  "romania",     "industry": "accounting",     "likes":    []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "_id":  "yo\xc7\xd7\x1a\xa9qy1['\xea",     "name": "ediloc.com",     "city": "calimanesti", }, {     "name":        "ediloc.com2",     "industry":    "automotive",     "description": "<p>sasdfdasf</p>",     "likes":       []interface {}{         "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     },     "user_id": "yo\xc7;\x1a\xa9qy\b\xb8\xa2\xf9",     "city":    "calimanesti",     "country": "romania",     "profile": []interface {}{         "rkwmmxpwhegczwvgn2tzsru7jrorhorkwmmxpwhegczwvgn2tzsru7jrorho=0.jpg",     },     "nrlikes": int(1),     "_id":     "yo\xd4f\x1a\xa9q|w\tg^", }, } 

pro , check fields in32, documents higher pro field number should have priority on documents have higher check fields.

make sure have sort pipeline stage before limit & skip stages. can reliably obtain same results limit/skip on sorted input.

edit

realised using bson.m {"pro": -1, "check": -1} define sort order. iteration order of map unspecified in go , can change. hence why getting inconsistent results.

try changing bson.d order of columns sort maintained.

it make see how query sort method constructs strings provide.

for use case, change or variable to:

or := bson.m{     "$sort": bson.d{         bson.docelem{name: "pro", value: -1},         bson.docelem{name: "check", value: -1},      }, } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -