How to query $in

5,959 views
Skip to first unread message

zeroc8

unread,
Nov 15, 2011, 5:49:10 AM11/15/11
to mgo-...@googlegroups.com
Hi, my mongo query looks like this, but I"m not quite sure how to formulate this using mgo:

Normally, I would find a user like so:
var result User = {}
err = c.Find(bson.M{"user":user, "passwd":passwd}).One(&result)        

But now, I"d like to whitelist the user fritz, my query looks like this within the mongo client.
I cannot just pass the query expression, as it contains a $ sign:

db.users.find({user:"fritz",imeis: {$in:["300015010802830"]} })

How can I use this query from Go?







Paddy Foran

unread,
Nov 15, 2011, 5:52:35 AM11/15/11
to mgo-...@googlegroups.com
I got $or by just using it in quotes. So

c.Find(bson.M{user:"fritz",imeis:{"$in":["3000....."]}})

Hope that helps.

Thanks,
Paddy Foran

zeroc8

unread,
Nov 15, 2011, 6:45:40 AM11/15/11
to mgo-...@googlegroups.com
Thanks, for the tip, unfortunately I still cannot compile this:

err = c.Find(bson.M {user:"fritz", imeis: {"$in" : ["10","11"] } }).One(&result)

syntax error: unexpected comma, expecting ]


Paddy Foran

unread,
Nov 15, 2011, 6:57:50 AM11/15/11
to mgo-...@googlegroups.com
Sorry, your Array syntax is wrong.
["10","11"] needs to be [2]string{"10","11"}, I believe.


Thanks,
Paddy Foran

Gustavo Niemeyer

unread,
Nov 15, 2011, 10:51:38 AM11/15/11
to mgo-...@googlegroups.com
> ["10","11"] needs to be [2]string{"10","11"}, I believe.

That's right. As a hint, this is commonly used without the number, so
you get a slice out rather than an array (as in, []string{"10",
"11"}).

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

zeroc8

unread,
Nov 15, 2011, 2:33:25 PM11/15/11
to mgo-...@googlegroups.com
Yeah, I guess I still need to get used to Go.

Anyway, after your suggesions I tried this:

err = c.Find(bson.M {user:"fritz", imeis: {"$in" : []string{"10","11"} } }).One(&result)

Which doesn't seem to work. I also tried various other combinations (declaring the string array up front and such), couldn't get those to work.

Later I realized that I don't really have the need to query for more than one imei, so that did it for now.
I'm still wondering why the sampe above doesn't work though.

Thanks for you work Gustavo, I'm really enjoying this combination of Go, Mongo and mgo.



Gustavo Niemeyer

unread,
Nov 15, 2011, 3:25:10 PM11/15/11
to mgo-...@googlegroups.com
> Anyway, after your suggesions I tried this:
> err = c.Find(bson.M {user:"fritz", imeis: {"$in" : []string{"10","11"} }
> }).One(&result)

bson.M is a Go map type. The keys must be variables or constants
rather than field names (bson.M{"foo": "bar"}, etc).

> Thanks for you work Gustavo, I'm really enjoying this combination of Go,
> Mongo and mgo.

Nice, glad to hear that!

zeroc8

unread,
Nov 16, 2011, 7:27:18 AM11/16/11
to mgo-...@googlegroups.com
So ok, it seems to work, just to make sure I"m doing this the way it"s supposed to:

If I want something similar to
select * from users where user="myuser" and imeis in ('imei1','imei2')

I could write this like this:

var myarr = []string{"imei1","imei2"}
err = c.Find(bson.M {"user":"myuser", "imeis": bson.M {"$in":myar} }).One(result)

Is that correct?

Gustavo Niemeyer

unread,
Nov 29, 2011, 9:39:52 AM11/29/11
to mgo-...@googlegroups.com
> So ok, it seems to work, just to make sure I"m doing this the way it"s
> supposed to:
(...)
> Is that correct?

That should be right. In that style, you can map any of the queries
described at the documentation precisely:

http://www.mongodb.org/display/DOCS/Advanced+Queries

Rakesh Kaupu

unread,
Jun 23, 2017, 3:32:41 PM6/23/17
to mgo-users
"err = c.Find(bson.M {"user":"myuser", "imeis": bson.M {"$in":myar} }).One(result)"
Did this query work for you ?
I'm trying to do the same (find string in a array variable) but I get an error from bson "Unmarshal needs a map or a pointer to a struct."
I do realize I'm replying on really old post but I can't seem to find solution anywhere.
Thanks
Reply all
Reply to author
Forward
0 new messages