Query from an Array of strings

27 views
Skip to first unread message

Nick Beenham

unread,
Apr 26, 2016, 3:29:20 PM4/26/16
to mgo-users
hi all,

I'm having some problems querying inside an array. I'm trying to query a document based on whether a user is contained within an array

//Subscription - struct
type Subscription struct {
Poller    string   `json:"poller"`
AppName   string   `json:"appName"`
Channels  []string `json:"channels"`
Users     []string `json:"users"`
Squelched string   `json:"squelched"`
}

var subs []Subscription
var userValue = "myuser"
query := bson.M{"users": bson.M{"$elemMatch": bson.M{"$in": userValue}}}

err := collection.Find(query).All(&subs)


Any ideas?

Thanks in advance

Nick

Poletaev Roman

unread,
May 16, 2016, 3:48:04 PM5/16/16
to mgo-users
maybe so?
query := bson.M{"users": bson.M{"$in": []string{userValue}}}

вторник, 26 апреля 2016 г., 22:29:20 UTC+3 пользователь Nick Beenham написал:

Gustavo Niemeyer

unread,
May 16, 2016, 3:51:42 PM5/16/16
to mgo-...@googlegroups.com
Hi Nick,

Your probably is likely not the array, but the fact the fields are decorated with json tags only. The bson package takes field tags prefixed by bson: rather than json:

--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages