Distinct and sorting

30 views
Skip to first unread message

Darko Luketic

unread,
Nov 4, 2017, 3:53:07 PM11/4/17
to mgo-users
Hello,

resurrecting the dead here.

I have a problem with distinct query and sorting.

type Node struct {
Tags []*Tag
}

type Tag struct {
Slug string
Name string
}

and this function

func (h *Handler) allTags() ([]*model.Tag, error) {
ms := h.ms.Copy()
defer ms.Close()
c := ms.DB(h.db).C(contentCollection)
tags := []*model.Tag{}
if e := c.Find(nil).Distinct("tags", &tags); e != nil {
return nil, e
}
return tags, nil
}

and I tried .Sort("tags") or "tags.name" "tags.slug" but it's not working.
And I can't suffix another function after distinct.

I guess I have to manually sort the result, or is there a way?

Regards,
Darko

Diego Medina

unread,
Nov 6, 2017, 5:55:02 AM11/6/17
to mgo-users
the mongo server doesn't allow sorting when calling distinct so you'll have to sort it on your program


Depending on how large the list you get from mongodb, you can decide to sort it in your code tr rewrite the query using aggregate

Regards,

Diego
Reply all
Reply to author
Forward
0 new messages