Is anybody here using MongoTalk doing some fancy custom queries? Not just with boolean conditions but with regex for example.
I’m interested in being able to execute and get the results of a raw javascript query but I cannot found how to do it. I am missing something? |
> Am 30.12.2014 um 02:32 schrieb Sebastian Sastre <[hidden email]>: > > Is anybody here using MongoTalk doing some fancy custom queries? Not just with boolean conditions but with regex for example. > > I’m interested in being able to execute and get the results of a raw javascript query but I cannot found how to do it. > > I am missing something? 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 Hope that helps, Norbert |
that’s it. That really had helped :)
Thank you Norbert! > On Dec 30, 2014, at 10:14 AM, Norbert Hartl <[hidden email]> wrote: > > >> Am 30.12.2014 um 02:32 schrieb Sebastian Sastre <[hidden email]>: >> >> Is anybody here using MongoTalk doing some fancy custom queries? Not just with boolean conditions but with regex for example. >> >> I’m interested in being able to execute and get the results of a raw javascript query but I cannot found how to do it. >> >> I am missing something? > > 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 > > Hope that helps, > > Norbert > > |
In reply to this post by NorbertHartl
On Dec 30, 2014, at 7:14 AM, Norbert Hartl <[hidden email]> 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
|
On Jan 4, 2015, at 4:38 AM, Norbert Hartl <[hidden email]> wrote: > someCollection select: { > '$or' -> { > { 'name' -> { > '$regex' -> 'test' . > '$options' -> 'i' } asDictionary } asDictionary. > { 'description' -> { > '$regex' -> 'test' . > '$options' -> 'i' } asDictionary } asDictionary > } > } asDictionary That worked. I was close, but I was using $or: instead of $or, and I was missing one level of asDictionary. Just for the archive, there’s one more query I needed, which was to find an object based on it’s object id. I ended up using: voyageId := Integer readFrom: (request at: #voyageId) base: 16. someModel := SomeModel selectOne: { '_id' -> (OID value: voyageId) } asDictionary. This is a web app using Teapot and Mustache, so I write the voyageId out using: self voyageId value printStringHex |
> Am 05.01.2015 um 04:59 schrieb Yanni Chiu <[hidden email]>: > > > On Jan 4, 2015, at 4:38 AM, Norbert Hartl <[hidden email]> wrote: > >> someCollection select: { >> '$or' -> { >> { 'name' -> { >> '$regex' -> 'test' . >> '$options' -> 'i' } asDictionary } asDictionary. >> { 'description' -> { >> '$regex' -> 'test' . >> '$options' -> 'i' } asDictionary } asDictionary >> } >> } asDictionary > > That worked. I was close, but I was using $or: instead of $or, and I was missing one level of asDictionary. > > Just for the archive, there’s one more query I needed, which was to find an object based on it’s object id. I ended up using: > > voyageId := Integer readFrom: (request at: #voyageId) base: 16. > someModel := SomeModel selectOne: { '_id' -> (OID value: voyageId) } asDictionary. > > This is a web app using Teapot and Mustache, so I write the voyageId out using: > > self voyageId value printStringHex Sounds cool. If you have anything to publish I'm interested what you've done. Especially if you have some nice tricks doing form handling in teapot. Btw. for the archive. A common use case I encounter is to query objects that are being referenced by another object. It works like this AnyClass>>#referencedBy: anObject ^ AnyClass selectMany: { 'reference.__id' -> anObject voyageId } asDictionary Norbert |
Hi Norbert,
this reminds me of something IIRC that Alan Knight did in TopLink, a commercial OR mapping tool. Alan allowed one to write queries in conventional Smalltalk such as db select: [:obj| obj name = 'foo' or: [obj feature isSomething]] but under the covers compiling this block into a query that the database could execute by evaluating the block once, passing in a "compiling" object that implements doesNotUnderstand: to catch the messages sent in the block. That technique could work in this context and avoid introducing the (excuse me for saying, /horrible/) tuple syntax. Eliot (phone) On Jan 5, 2015, at 4:41 AM, Norbert Hartl <[hidden email]> wrote: > >> Am 05.01.2015 um 04:59 schrieb Yanni Chiu <[hidden email]>: >> >> >> On Jan 4, 2015, at 4:38 AM, Norbert Hartl <[hidden email]> wrote: >> >>> someCollection select: { >>> '$or' -> { >>> { 'name' -> { >>> '$regex' -> 'test' . >>> '$options' -> 'i' } asDictionary } asDictionary. >>> { 'description' -> { >>> '$regex' -> 'test' . >>> '$options' -> 'i' } asDictionary } asDictionary >>> } >>> } asDictionary >> >> That worked. I was close, but I was using $or: instead of $or, and I was missing one level of asDictionary. >> >> Just for the archive, there’s one more query I needed, which was to find an object based on it’s object id. I ended up using: >> >> voyageId := Integer readFrom: (request at: #voyageId) base: 16. >> someModel := SomeModel selectOne: { '_id' -> (OID value: voyageId) } asDictionary. >> >> This is a web app using Teapot and Mustache, so I write the voyageId out using: >> >> self voyageId value printStringHex > > Sounds cool. If you have anything to publish I'm interested what you've done. Especially if you have some nice tricks doing form handling in teapot. > > Btw. for the archive. A common use case I encounter is to query objects that are being referenced by another object. It works like this > > AnyClass>>#referencedBy: anObject > ^ AnyClass selectMany: { > 'reference.__id' -> anObject voyageId } asDictionary > > Norbert > > |
Hi Eliot,
> Am 05.01.2015 um 16:59 schrieb Eliot Miranda <[hidden email]>: > > Hi Norbert, > > this reminds me of something IIRC that Alan Knight did in TopLink, a commercial OR mapping tool. Alan allowed one to write queries in conventional Smalltalk such as > > db select: [:obj| obj name = 'foo' or: [obj feature isSomething]] > > but under the covers compiling this block into a query that the database could execute by evaluating the block once, passing in a "compiling" object that implements doesNotUnderstand: to catch the messages sent in the block. > > That technique could work in this context and avoid introducing the (excuse me for saying, /horrible/) tuple syntax. > We have that, too. It is called MongoQueries and has been done by Nicholas Petton. For basic usage like your example above it works perfectly. But if it comes to special nested accessors or complex queries you end up having problems really quick. I think with a little love the MongoQueries package can easily be extended to support a fair amount of use cases. You are right, the nested dictionaries are awful but those smalltalk DSL mapping queries are really restricted and hard to make them powerful. So it is again the choice between beelzebub and the devil (as we say). Norbert > Eliot (phone) > > On Jan 5, 2015, at 4:41 AM, Norbert Hartl <[hidden email]> wrote: > >> >>> Am 05.01.2015 um 04:59 schrieb Yanni Chiu <[hidden email]>: >>> >>> >>> On Jan 4, 2015, at 4:38 AM, Norbert Hartl <[hidden email]> wrote: >>> >>>> someCollection select: { >>>> '$or' -> { >>>> { 'name' -> { >>>> '$regex' -> 'test' . >>>> '$options' -> 'i' } asDictionary } asDictionary. >>>> { 'description' -> { >>>> '$regex' -> 'test' . >>>> '$options' -> 'i' } asDictionary } asDictionary >>>> } >>>> } asDictionary >>> >>> That worked. I was close, but I was using $or: instead of $or, and I was missing one level of asDictionary. >>> >>> Just for the archive, there’s one more query I needed, which was to find an object based on it’s object id. I ended up using: >>> >>> voyageId := Integer readFrom: (request at: #voyageId) base: 16. >>> someModel := SomeModel selectOne: { '_id' -> (OID value: voyageId) } asDictionary. >>> >>> This is a web app using Teapot and Mustache, so I write the voyageId out using: >>> >>> self voyageId value printStringHex >> >> Sounds cool. If you have anything to publish I'm interested what you've done. Especially if you have some nice tricks doing form handling in teapot. >> >> Btw. for the archive. A common use case I encounter is to query objects that are being referenced by another object. It works like this >> >> AnyClass>>#referencedBy: anObject >> ^ AnyClass selectMany: { >> 'reference.__id' -> anObject voyageId } asDictionary >> >> Norbert >> >> > |
Free forum by Nabble | Edit this page |