I need to do a query in an Cloudant DataBase where you compare a number with decimals that is defined as a string with another number sent from the server. The problem is that a comparison of strings is made and I need it to be a numerical comparison. There is there any way to perform this search by converting the database parameter to float while doing the query? O there are another way to do this query?
This is the query in the server, value.precio is sent from the client as a string.
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
};
And in my database this is the parameter I want to search is:
"precio": "13.39"
Thanks
I don't think you will be able to do this with Cloudant Query, but you could try Cloudant Search. Create a new search index similar to the following:
Design Doc: myDesignDoc
Index Name: byPrecio
Index:
function (doc) {
if (doc.precio) {
index("precio", parseFloat(doc.precio));
}
}
Then you can uses ranges to search. For example:
precio:[13 TO 14]
Full search on Cloudant would look like this:
https://xxx.cloudant.com/YOUR_DB/_design/myDesignDoc/_search/byPrecio?q=precio:[13%20TO%2014]&include_docs=true
Sample response:
{
"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
Related
I am using cosmosDB to do a $geowithin lookup and when i run the query in the cosmosdb data explorer it works fine and returns the right set. When i run it in node js using the MongoClient I get back a empty set.
This is the query i ran in cosmosdb data explorer:
{CurrentLoc: {
$geoWithin: {
$centerSphere: [[-122.0312186,37.33233141], 0.0025232135647961246]
}
}}
That returned the correct list of documents
This is in my node app:
var toFind = {
CurrentLoc: {
$geoWithin: {
$centerSphere: [currentLoc, maxDistance]
}
}
}
var query = db.collection('User').find(toFind)
query.toArray(function (err, doc, queryResult) {
logger.debug ('result: ' + JSON.stringify(doc);
})
That returns a blank set and does not get the data back. Why would it be returning different things and also why is it blank?
You might need to break down the currentLoc variable into an array of two values, so the query is structured in the same manner as when executed through CosmosDB Data Explorer:
[-122.0312186,37.33233141] in
$centerSphere: [[-122.0312186,37.33233141], 0.0025232135647961246]
Hope it helps!
I have a Json like this:
"datos_personales":
{
"nombre":"Dionel",
"apellido":"Delgado",
"fechanacimiento":"1990-12-31T04:00:00.000Z",
"lugarNacimiento":"Venezuela, Maracaibo",
"edad":25,
"genero":"Masculino",
"cedula":"21076754",
"direccion":"San Carlos",
"telefonofijo":"0262-6871111",
"telefonomovil":"0262-6871111"
},
"datos_emergencia":
{
"nombre1":"Jeaynie",
"apellido1":"Valbuena",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"parentesco1":"Madre",
"nombre2":"Diones",
"apellido2":"Delgado",
"telefono3":"0262-6871111",
"telefono4":"0262-6871111",
"parentesco2":"Padre"
},
"datos_sociales":
{
"civil":"Soltero",
"estudios4":true,
"ocupacion":"Programador Web",
"hijos":"No"
},
"datos_medicotratante":
{
"nombre":"Naikelin",
"apellido":"Ruiz",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"especialidad":"PediatrĂa",
"sangre":"AB",
"rh":"Negativo",
"seguro":"No"
}
`
I need to query these kinds of documents with nodeJS using "cedula" to find them
{"datos_personales.cedula":21076754} << in this case
I tried using this but it doesn't fetch the document
var ced = 21076754;
db.getCollection('users').find({"datos_personales.cedula":ced});
any suggestions?
"cedula":"21076754",
Your result is a string (See the quotes).
Change your query to:
var ced = "21076754";
db.getCollection('users').find({"datos_personales.cedula":ced});
Another solution to the problem is -
find() query in MongoDB returns a cursor while findOne() query returns a JSON Object which matches the argument passed in the query. You can retrieve the desired record by using the query below -:
db.getCollection('users').findOne({"datos_personales.cedula":ced});
I create an index using the below function:
function (doc) {
if(doc.type === 'Property') {
if(doc.Beds_Max) {
try {
index("Beds_Max", parseInt(doc.Beds_Max));
}
catch(err) {
//ooopss
}
}
if(doc.YearBuilt) {
try {
index("YearBuilt", parseInt(doc.YearBuilt));
}
catch(err) {
//ooopss
}
}
}
}
using the cloudant Design Documents -> New Search Index and after the index is built I can issue queries like
"YearBuilt": [2010 TO Infinity]
But if I try to query the same index using Cloudant query I see weird behavior. If I go to Cloudant Dashboard -> Query and pass something like
{"limit": 5,
"selector": {
"_id": {
"$gt": null
},
"Beds_Max": {"$gte": 7}
},
fields: ["_id"]}
I see huge spike in data transmission, it keeps on receiving huge amounts of data even for the most unusual queries which are only supposed to return no more than 1 or 2 results and then hangs my computer so that most probably is not right. When I use Pouchdb-find npm module which has support for Cloudant 2.0 Query and issue the same selector as above I see inconsistent behavior, e.g. sometimes it returns 0 rows and sometimes it gives a ETIMEOUTERROR. If I change the index and exclude parseInt I can query using the same Pouchdb-find and even Cloudant Dashboard-> Query and get the results but in that case I lose the ability to use inequality operators which is a no go for me.
I'm open to work-arounds and even altogether different features to achieve the desired result.
Am new to nodejs here and I have below data in my session which will be available during page post
{"lstDetails":
[
{"ID":"FFSDER2da2411cDSs12CGh21",
"FirstName":"Test",
"LastName":"Data",
"DOB":"8/15/1921 12:00:00 AM",
"AddressLine1":"Test Address Line 1",
"AddressLine2":"",
"City":"FakeCity",
"State":"ST",
"Zip":"41511",
"PID":0,
"EmailID":"SC4239925#FakeEmail.com",
"Gender":"1",
"WorkPhone":"",
"OtherPhone":"5555555555",
"ICarier":
{"ICName":null,
"IGName":null,
"IGNum":null
}
},
{"ID":"DS24DASD5da21afd56D4#2!",
"FirstName":"Test2",
"LastName":"Data2",
"DOB":"8/15/1921 12:00:00 AM",
"AddressLine1":"Test2 Address Line 1",
"AddressLine2":"",
"City":"FakeCity2",
"State":"ST2",
"Zip":"41511",
"PID":0,
"EmailID":"SC4239925#FakeEmail.com",
"Gender":"1",
"WorkPhone":"",
"OtherPhone":"5555555555",
"ICarier":
{"ICName":null,
"IGName":null,
"IGNum":null
}
}
]
}
and the above detail will be stored in req.Session. How can search for particular data from above session object using ID and fetch a particular record? I have went on through net, but unfortunately did not find any useful information.
You can use either filter or map to search through the array to find the object (node) you want.
Simplified example:
var myArray = [{
"ID":"FFSDER2da2411cDSs12CGh21",
"FirstName":"Test",
"LastName":"Data"
},
{
"ID":"DS24DASD5da21afd56D4#2!",
"FirstName":"Test2",
"LastName":"Data2"
}];
var result = myArray.filter(function(f) {
if (f.ID === 'FFSDER2da2411cDSs12CGh21') {
return f;
}
});
Results in a result array with one object, filtered on ID. You can filter by comparing on part of string, RegEx, etc.
Say, I have the following query: 'document spreadsheet app*'. I want to construct the query dictionary which is equivalent to this type of query '_search?q=_all:document+spreadsheet+app*'. Say, query_words_list = ['document', 'spreadsheet', 'app']. I tried this:
{
'query': {
'terms': {
'_all': query_words_list[:-1] + [query_words_list[-1] + '*'],
}
}
}
However, if you compare results of two queries, they are not equivalent. Any hints what query dictionary equivalent to '_search?q=_all:document+spreadsheet+app*' can look like? Thanks in advance!
The q parameter is equivalent to Query String Query.