diff --git a/shock-server/controller/node/multi.go b/shock-server/controller/node/multi.go index e830605f..8affc088 100644 --- a/shock-server/controller/node/multi.go +++ b/shock-server/controller/node/multi.go @@ -72,7 +72,7 @@ func (cr *NodeController) ReadMany(ctx context.Context) error { // Gather params to make db query. Do not include the following list. if _, ok := query["query"]; ok { - paramlist := map[string]int{"query": 1, "limit": 1, "offset": 1, "order": 1, "direction": 1} + paramlist := map[string]int{"query": 1, "limit": 1, "offset": 1, "order": 1, "direction": 1, "distinct": 1} for key := range query { if _, found := paramlist[key]; !found { keyStr := fmt.Sprintf("attributes.%s", key) @@ -86,7 +86,7 @@ func (cr *NodeController) ReadMany(ctx context.Context) error { } } } else if _, ok := query["querynode"]; ok { - paramlist := map[string]int{"querynode": 1, "limit": 1, "offset": 1, "order": 1, "direction": 1, "owner": 1, "read": 1, "write": 1, "delete": 1, "public_owner": 1, "public_read": 1, "public_write": 1, "public_delete": 1} + paramlist := map[string]int{"querynode": 1, "limit": 1, "offset": 1, "order": 1, "direction": 1, "distinct": 1, "owner": 1, "read": 1, "write": 1, "delete": 1, "public_owner": 1, "public_read": 1, "public_write": 1, "public_delete": 1} for key := range query { if _, found := paramlist[key]; !found { for _, value := range query[key] { @@ -144,6 +144,23 @@ func (cr *NodeController) ReadMany(ctx context.Context) error { // Combine permissions query with query parameters and ACL query into one AND clause q["$and"] = []bson.M{qPerm, qOpts, qAcls} + // process distinct query + if _, ok := query["distinct"]; ok { + dField := query.Get("distinct") + if !node.HasAttributeField(dField) { + err_msg := "err unable to run distinct query on non-indexed attributes field: " + dField + logger.Error(err_msg) + return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) + } + results, err := node.DbFindDistinct(q, dField) + if err != nil { + err_msg := "err " + err.Error() + logger.Error(err_msg) + return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) + } + return responder.RespondWithData(ctx, results) + } + // defaults order := "created_on" direction := "-" diff --git a/shock-server/node/db.go b/shock-server/node/db.go index fe1f28cb..d826901b 100644 --- a/shock-server/node/db.go +++ b/shock-server/node/db.go @@ -38,6 +38,15 @@ func Initialize() { } } +func HasAttributeField(a string) bool { + for _, b := range strings.Split(conf.MONGODB_ATTRIBUTE_INDEXES, ",") { + if a == strings.TrimSpace(b) { + return true + } + } + return false +} + func dbDelete(q bson.M) (err error) { session := db.Connection.Session.Copy() defer session.Close() @@ -77,6 +86,14 @@ func dbFind(q bson.M, results *Nodes, order string, options map[string]int) (cou return } +func DbFindDistinct(q bson.M, d string) (results interface{}, err error) { + session := db.Connection.Session.Copy() + defer session.Close() + c := session.DB(conf.MONGODB_DATABASE).C("Nodes") + err = c.Find(q).Distinct("attributes."+d, &results) + return +} + func Load(id string) (n *Node, err error) { session := db.Connection.Session.Copy() defer session.Close()