How to use the "OR" operator?

2,796 views
Skip to first unread message

TheOnly92

unread,
Jun 15, 2011, 4:16:43 AM6/15/11
to mgo-users
{$or: [{status: 1}, {status: 0}]}, I write this way in JSON, but how
do I write it in bson?

I can't bson.M{"$or": bson.M{bson.M{"status":0},
bson.M{"status":nil}}}, right? It gives me: missing key in map literal

I can't use bson.M{"$or": bson.D{{"status",1}, {"status",nil}}} as
well, crashes with: $or requires nonempty array

Fabian

unread,
Jul 10, 2011, 12:20:07 PM7/10/11
to mgo-users
I have quite the same problem - how to use conditional operators.
If I've not missed something, this is not possible at the moment - so
I can't even filter my results by the existance of a specific field.
Of course one could do these things in a For-Func but before I do that
I better ask whether I've just failed to see the right function.

For the case that I'm right - would it be possible to call
Collection.Find() with json data as a string? This would make things a
lot easier and clearer for complex queries.

Gustavo Niemeyer

unread,
Jul 10, 2011, 4:15:37 PM7/10/11
to mgo-...@googlegroups.com
Hi Fabian,

> I have quite the same problem - how to use conditional operators.
> If I've not missed something, this is not possible at the moment - so

I believe gobson is able to represent pretty much anything you can do
with the bson standard, so any query you find floating around you can
execute using gobson.

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

-- I never filed a patent.

Gustavo Niemeyer

unread,
Jul 10, 2011, 4:49:15 PM7/10/11
to mgo-...@googlegroups.com

Hey there,

You have to reproduce the same query you have in JSON.  If JSON has a list, you'll need a list in bson as well, which is represented by slices in Go, as usual.  If you want a generic list for querying purposes, you can easily define a type like this locally:

type L []interface{}

Fabian Reinartz

unread,
Jul 10, 2011, 6:17:09 PM7/10/11
to mgo-...@googlegroups.com
Okay, it finally worked. No idea what went wrong before. 
Thanks for your fast help :)

Jon Conradt

unread,
May 29, 2012, 7:12:10 PM5/29/12
to mgo-...@googlegroups.com
Would you mind posting the resulting code?  I am running into the same problem.  Basically, I want to have $or with a list of potential values for the same key and I can't seem to get the syntax right.

Thanks!

Jon

Jordan Orelli

unread,
May 31, 2012, 10:31:25 AM5/31/12
to mgo-...@googlegroups.com
well what you have to remember is that you're writing in native Go types, and your goal is to have it serialize to the $or semantics.  There are multiple ways to arrive at such a result, but the one I common use is this:

bson.M{"$or": []bson.M{bson.M{"somekey": "someval"}, bson.M{"otherkey": "otherval"}},

the value of an "$or" is an array of selectors.

Likewise, the $in modifier is used like this:

bson.M{"first_name": bson.M{"$in": []string{"Stanley", "Herbert", "Alfred"}}}

(based on your stated goal, it sounds like you're actually looking for the $in modifier)

Jon Conradt

unread,
May 31, 2012, 11:53:24 AM5/31/12
to mgo-...@googlegroups.com
Thanks!  My practice project has me learning Go and Mongo at the same time.  The pointer to look at $in is really helpful.

It is always frustrating when you read through a thread as two people are working on the same problem you are facing and the last message just says, "Got it to work!"  Having the working snippet of code is a big help.  :)

Jon

서원기 차장

unread,
Jun 3, 2012, 9:40:44 PM6/3/12
to mgo-...@googlegroups.com
hi  i am wonki 
programing in korea
 
i send code source  
mongo write protocol use delphi .. BSON ..
but query  "or" operation not use ..
 
I want code  "or" query code samiple ....
ex)  {$or:[{DKEY:{$in:['1869','1870']}},{STATECD:{$in:['0102','0104']}}]}  -> BSON Format (  BSON([ 'DKEY', BSON(acdkey) ]); )..
 
Tanks!  Tanks!  ^^

2012/5/30 Jon Conradt <j...@theconradts.com>
CmdLog.zip

Gustavo Niemeyer

unread,
Jun 4, 2012, 9:45:54 AM6/4/12
to mgo-...@googlegroups.com
Hello there,

Sorry, but I can't really follow what you are asking about or trying to achieve.

If you want a code sample for an $or query, Jordan already sent one to
this thread, and pretty much any query you find in the actual MongoDB
documentation can also be used with mgo by translating the curly
braces to the bson.M{} syntax.

I hope this helps.
gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages