2017-10-06 49 views
0

我需要在Cloudant DataBase中执行查询,在该数据库中将数字与定义为字符串的小数与服务器发送的另一个数字进行比较。问题在于比较字符串,我需要它作为数字比较。在执行查询时,通过将数据库参数转换为float可以执行此搜索吗? O还有另一种方法来做这个查询?在Cloudant查询中将字符串参数转换为浮点数

这是服务器中的查询,value.precio作为字符串从客户端发送。

 value.precio = value.precio.split("-"); 
     var precio_init = value.precio[0]; 
     var precio_final = value.precio[1]; 

     value.precio = { 
      "$gte":precio_init, 
      "$lte":precio_final 
     }; 

而且在我的数据库,这是我想要搜索的参数为:

“PRECIO”: “13.39”

感谢

+0

你能提供一个具体的例子和说明你的文件的相关部分正试图查询? – ptitzler

+0

我编辑了我的问题......我希望这已经足够了谢谢 –

回答

0

我不认为你可以使用Cloudant Query执行此操作,但您可以尝试Cloudant Search。创建类似于下面的一个新的搜索索引:

设计文件:myDesignDoc

指数名称:byPrecio

指数:

function (doc) { 
    if (doc.precio) { 
    index("precio", parseFloat(doc.precio)); 
    } 
} 

然后你可以使用范围进行搜索。例如:

precio:[13 TO 14] 

上Cloudant全搜索应该是这样的:

https://xxx.cloudant.com/YOUR_DB/_design/myDesignDoc/_search/byPrecio?q=precio:[13%20TO%2014]&include_docs=true

样本响应:

{ 
    "total_rows":1, 
    "bookmark":"g2wAAAAxxx", 
    "rows":[ 
    { 
     "id":"74fa6ff1b6dbca8c10d677832f6a3de2", 
     "order":[ 
     1.0, 
     0 
     ], 
     "fields":{ 

     }, 
     "doc":{ 
     "_id":"74fa6ff1b6dbca8c10d677832f6a3de2", 
     "_rev":"2-17c984e51102b719fe9f80fc5d5bc78e", 
     "precio":"13.39", 
     "otherField":"otherValue" 
     } 
    } 
    ] 
} 

More info on Cloudant Search here

+0

嗨,我没有试图在云查询中进行转换。我使用了你给我们的设计文档,但是云查询precio:[13到14]不起作用。感谢您的回复 –

+0

这使用Cloudant Search而不是Cloudant Query。你在使用Cloudant Search吗? https://console.bluemix.net/docs/services/Cloudant/api/search.html#search – markwatsonatx

相关问题