Am 04.01.2015 um 05:12 schrieb Yanni Chiu <yanni@rogers.com>:


On Dec 30, 2014, at 7:14 AM, Norbert Hartl <norbert@hartl.name> wrote:


In order to execute a javascript query you would need a javascript interpreter. As this is not feasible Mongo provides alternative constructs for the most important things you need. For the regex it would be

aMongoCollection select: {
'fieldName' -> { '$regex' -> '.*foo' } asDictionary
} asDictionary

What would be the equivalent of:

db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, { description: { $regex: /test/i } } ] } )

In other words, how would a query on multiple fields be done in MongoTalk?

The case-insensitive match can probably done with "$options:� (just tried it, and it works).
MyMongoClass selectMany: {
'description' -> { '$regex' -> 'YADA'. '$options' -> 'i' } asDictionary
} asDictionary.


Just as you wrote it. Using $regex and // is not necessary. Slash is the regex operator in javascript. As an alternative you use $regex and the search term in a string. 

/test/

becomes

{ '$regex' -> 'test' } asDictionary

Every { "key" : "value" } object in javascript you need to convert to { 'key' -> 'value' } asDictionary and every Array [ "val", "val" ] into  { 'val' . 'val' }. And regex modifiers go into $options as you found out already. So the complete query would be like.

someCollection select: { 
'$or' -> { 
{ 'name' -> { 
'$regex' -> 'test' . 
'$options' -> 'i' } asDictionary } asDictionary.
{ 'description' -> { 
'$regex' -> 'test' . 
'$options' -> 'i' } asDictionary } asDictionary
} asDictionary

Norbert