marcesher / cfmongodb

MongoDB client wrapper for ColdFusion
89 stars 34 forks source link

Geo queries fails with maxDistance #24

Open jdaury opened 12 years ago

jdaury commented 12 years ago

Geoqueries like $near and $nearSphere often fails if $maxDistance is specified, returning the following error:

-- geo values have to be numbers: { $maxDistance: 0.00391972405142678, $nearSphere: [ 45.46427, 9.18951 ] }

this geo query is order sentisive and maxDistance should not precede nearSphere

the following query returns the correct results 'Position' : { $nearSphere: [ 45.46427, 9.18951 ] ,$maxDistance: 0.00391972405142678}

marcesher commented 12 years ago

What engine are you seeing this on: Adobe CF? Railo?

Please submit a self-contained example that clearly demonstrates the issue.

Thanks!

jdaury commented 12 years ago

Hi! i'm on Railo 3.3, here's an example code:

mongoConfig = createObject('component','com.cfmongodb.core.MongoConfig').init(dbName="db"); mongo = createObject('component','com.cfmongodb.core.Mongo').init(mongoConfig); mongoCollection = mongo.getDBCollection("collection"); queryParams = { criteria={}, skip=10, limit=1000 }; queryParams.criteria['Position']['$nearSphere'] = [45.46427, 9.18951]; queryParams.criteria['Position']['$maxDistance'] = 0.00391972405142678; // (25 km / 6378 km) query = mongoCollection.find(criteria=queryParams.criteria,skip=queryParams.skip,limit=queryParams.limit); writeDump(query.asArray());

Cause: com.mongodb.MongoException Code: 13026 ErrorCode: 0 ExtendedInfo
Message: geo values have to be numbers: { $maxDistance: 0.00391972405142678, $nearSphere: [ 45.46427, 9.18951 ] } StackTrace: string geo values have to be numbers: { $maxDistance: 0.00391972405142678, $nearSphere: [ 45.46427, 9.18951 ] } at com.mongodb.MongoException.parse(MongoException.java:82):82 at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:312):312 at com.mongodb.DBCursor._check(DBCursor.java:369):369 at com.mongodb.DBCursor._hasNext(DBCursor.java:498):498 at com.mongodb.DBCursor.hasNext(DBCursor.java:523):523 at sun.reflect.GeneratedMethodAccessor91.invoke(Unknown Source):-1 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source):-1 at java.lang.reflect.Method.invoke(Unknown Source):-1 at railo.runtime.reflection.pairs.MethodInstance.invoke(MethodInstance.java:37):37 at railo.runtime.reflection.Reflector.callMethod(Reflector.java:626):626 at railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:742):742 at railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1444):1444 at com.cfmongodb.core.searchresult_cfc$cf.udfCall(C:\railo\webapps\ROOT\com\cfmongodb\core\SearchResult.cfc:34):34 at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:214):214 at railo.runtime.type.UDFImpl._call(UDFImpl.java:418):418 at railo.runtime.type.UDFImpl.call(UDFImpl.java:383):383 at railo.runtime.ComponentImpl._call(ComponentImpl.java:609):609 at railo.runtime.ComponentImpl._call(ComponentImpl.java:496):496 at railo.runtime.ComponentImpl.call(ComponentImpl.java:1794):1794 at railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:733):733 at railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:1444):1444 at index_cfm$cf.call(C:\railo\webapps\ROOT\index.cfm:19):19 at railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:762):762 at railo.runtime.listener.ModernAppListener._onRequest(ModernAppListener.java:179):179 at railo.runtime.listener.MixedAppListener.onRequest(MixedAppListener.java:23):23 at railo.runtime.PageContextImpl.execute(PageContextImpl.java:1991):1991 at railo.runtime.PageContextImpl.execute(PageContextImpl.java:1958):1958 at railo.runtime.engine.CFMLEngineImpl.serviceCFML(CFMLEngineImpl.java:297):297 at railo.loader.servlet.CFMLServlet.service(CFMLServlet.java:32):32 at javax.servlet.http.HttpServlet.service(HttpServlet.java:91):91 at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103):103 at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:175):175 at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:240):240 at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:263):263 at com.caucho.server.port.TcpConnection.run(TcpConnection.java:481):481 at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:685):685 at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:607):607 at java.lang.Thread.run(Unknown Source):-1

the case sensitiveness is intentional in mongodb: https://jira.mongodb.org/browse/SERVER-4696

marcesher commented 12 years ago

Thanks for the details. Your problem had the feel of Railo about it, which is why I asked.

Railo has an arg to StructNew which should tell it to create an ordered struct: http://wiki.getrailo.org/wiki/FUNCTION:STRUCTNEW

So for your queryParams, try this:

criteria = structNew("linked"); and then populate your criteria struct and add it to queryParams.

If that doesn't work, try creating the entire queryParams struct as a linked struct

Let me know if that fixes it

jdaury commented 12 years ago

Hi, unfortunatelly the linked structnew didn't solved the issue. by the way, i've done a little research about that and i've managed to solve the issue by the creation of a LinkedHashMap:

queryParams = createObject("java", "java.util.LinkedHashMap")

marcesher commented 12 years ago

On Sun, Mar 4, 2012 at 12:04 PM, jdaury reply@reply.github.com wrote:

Hi, unfortunatelly the linked structnew didn't solved the issue. by the way, i've done a little research about that and i've managed to solve the issue by the creation of a LinkedHashMap:

queryParams = createObject("java", "java.util.LinkedHashMap")

You can also use cfmongodb's "newDBObject()" function to get an ordered map:

mongoUtil.newDBObject().append( key, value ).append( key, value );

This will give you full type safety in addition to ordered keys


Reply to this email directly or view it on GitHub: https://github.com/marcesher/cfmongodb/issues/24#issuecomment-4311167