Filter data in CouchDB as IN in MySQL - couchdb

I am wondering if i could filter data in Couchdb similar with IN in MySQL. For example the map function is :
function(doc) {
emit(doc.idWord, [doc.idTwitterData, doc.tf*doc.idf]);
}
I want to select only documents that have idWord with values 1 or 5 for example. I tried to set startkey=1 and endkey=5 but it is not working.

It's very simple. All you need to query is ?keys=[1,5]. This will fetch all the records with the idWord equal to 1 or 5. You might want to encode the [ ]
As an addition: you use the parameters startkey and endkey if you are doing a ranged query. Here is a good explanation how a ranged query works.

Related

Get multiple documents from collection using nodejs and mongodb

Hi I have two mongodb collections. The first one returns json data (array) and with the output of this, I want to return documents that match.
When I run Console.log (req.bidder.myBids) I get the following output:
[{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3010},{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3020},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1040},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1050},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1060},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1070},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1090},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1100}]
The productId has duplicates, I want to remove duplicates and then call a routine that finds all the products that match and output as json.
So far I have this code that only outputs one document, but cant figure out how to add the array of productId's and then fetch all corresponding products.
var agencyId = req.body.agencyId;
var productId = req.body.productId;
if (!validate.STRING(agencyId)) {
res.apiError(messages.server.invalid_request);
} else {
dbProduct.find({productId:{$in:['3798b537-9c7b-4395-9e41-fd0ba39aa984','4c4bd71c-6664-4d56-b5d3-6428fe1bed19']}
}).then(dbRes => {
console.log(dbRes);
Updated code and works with hard-wired productId and updated above code. Looking at how to get the array data and transpose replacing the hard-wired productId's
The $in operator is what you want. See the docs here: https://docs.mongodb.com/manual/reference/operator/query/in/

How to filter on view using complex query

I am trying to filter on a view which emits bookName and bookItem.
emit([doc.basicInfo.bookName,doc.basicInfo.bookItem], 1);
it gives me below result without any query:
{"total_rows”:10,”offset":0,"rows":[
{"id":"d4e5548fb01e6e2c559e702fe7b138ad","key":["correctaccouts","billing"],"value":1},
{"id":"863c46c645b6344719a08231606f2a7d","key":["credeaccount","system"],"value":1},
{"id":"68d39e64c406127960dc735e8167eee3","key":["credeaccount11","system"],"value":1},
{"id":"1ab4d31588d76a42e85b526a316074de","key":["mayankamazon","billing"],"value":1},
{"id":"3204f5db5df91886373f95995ce09a2d","key":["mayankazure","asset"],"value":1},
{"id":"452c040048fb2b779205b3785615d368","key":["mayankmaaa","system"],"value":1},
{"id":"23f01f7bc60c2c8f24f6b741584a69fa","key":["TEST_AWS_Delete212sss12","asset"],"value":1},
{"id":"f0093f474e0d50f046b9fdc9145bdc91","key":["vijeth-myteam111115555555","asset"],"value":1},
{"id":"c3bce8dd1482d841f445fbd617ba1db7","key":["vijeth-myteam11111555sss5555","asset"],"value":1},
{"id":"347479ba91696b73f4a57252cd00a358","key":["vijeth-myteamOnly","asset"],"value":1}
]}
Now I am trying to query on it using complex keys:
satrtkey=[{},"asset"]&endkey=[{},"asset"]
It should return me:
{"total_rows”:5,”offset":0,"rows":[
{"id":"3204f5db5df91886373f95995ce09a2d","key":["mayankazure","asset"],"value":1},
{"id":"23f01f7bc60c2c8f24f6b741584a69fa","key":["TEST_AWS_Delete212sss12","asset"],"value":1},
{"id":"f0093f474e0d50f046b9fdc9145bdc91","key":["vijeth-myteam111115555555","asset"],"value":1},
{"id":"c3bce8dd1482d841f445fbd617ba1db7","key":["vijeth-myteam11111555sss5555","asset"],"value":1},
{"id":"347479ba91696b73f4a57252cd00a358","key":["vijeth-myteamOnly","asset"],"value":1}
]}
But it still gives me all 10 records. I want to filter only records of type "asset".
To use key ranges, you must narrow down your research starting with the left fields to the right fields.
For example, if your key would be: [doc.basicInfo.bookItem,doc.basicInfo.bookName]
You could search with start_key=["asset",null]&end_key=["asset",{}]
Also, your current query is equivalent to key=[{},"asset"]. Instead, you should have tried: start_key=[null,"asset"]&end_key=[{},"asset"] but it should not work.
Example
View:
function (doc) {
emit([doc.basicInfo.bookItem,doc.basicInfo.bookName], 1);
}
Query:
http://localhost:5984/<db>/_design/<design_name>/_view/<view_name>?include_docs=true&inclusive_end=true&start_key=%5B%22asset%22%2Cnull%5D&end_key=%5B%22asset%22%2C%7B%7D%5D

Couchdb - date range + multiple query parameters

I want to be able query the couchdb between dates, I know that this can be done with startkey and endkey (it works fine), but is it possible to do query for example like this:
SELECT *
FROM TABLENAME
WHERE
DateTime >= '2011-04-12T00:00:00.000' AND
DateTime <= '2012-05-25T03:53:04.000'
AND
Status = 'Completed'
AND
Job_category = 'Installation'
Generally-speaking, establishing indexes on multiple fields grows in complexity as the number of fields increases.
My main question is: do Status and Job_category need to be queried dynamically too? If not, your view is simple:
function (doc) {
if (doc.Status === 'Completed' && doc.Job_category === 'Installation') {
emit(doc.DateTime); // this line may change depending on how you break up and emit the datetimes
}
}
Views are fairly cheap, (depending on the size of your database) so don't be afraid to establish several that cover different cases. I would expect something like Status to have predefined list of available options, as oppposed to Job_category which seems like it could be more related to user input.
If you need those fields to be dynamic, you can just add them to the index as well:
function (doc) {
emit([ doc.Status, doc.Job_category, doc.DateTime ]);
}
Then you can use an array as your start_key. For example:
start_key=["Completed", "Installation", ...]
tl;dr: use "static" views where you have a predetermined list of values for a given field. while possible to query "dynamic" views with multiple fields, the complexity grows very quickly.

Compare values inside same subdocument for findOne() [MongoDB]

I have a database full of objects that look ~exactly like this (simplified for clarity):
{
"_id": "GIFT100",
"price": 100,
"priceHistory": [
100, 110
],
"update": 1444183299242
}
What I'm trying to do is create a query document for MongoJS (or MongoDB and I can figure out the rest) that looks for the fact that priceHistory[0] < priceHistory[1].
I would want my query document to return the above record as a result. Alternatively, I could change my document code to compare price < priceHistory[0] but I believe this still leads to the same problem (comparing values inside the same document).
Any help would be appreciated, I've exhausted my Google-foo.
Edit:
I want to return a set of records that indicate a price drop since our last scan (performed daily). Basically a set of "sale" items from a data source I don't control.
You can use the $where clause, but be careful--it's slow, it cannot use your indexes, and it will perform a full table scan. Pass on whatever Javascript you want to use for comparison:
db.collection.findOne({$where: "priceHistory[0] < priceHistory[1]"})
Additionally, you can skip the $where statement if that's the only thing you're querying by:
db.collection.findOne("priceHistory[0] < priceHistory[1]")

Does CouchDB support multiple range queries?

How are multiple range queries implemented in CouchDB? For a single range condition, startkey and endkey combination works fine, but the same thing is not working with a multiple range condition.
My View function is like this:
"function(doc){
if ((doc['couchrest-type'] == 'Item')
&& doc['loan_name']&& doc['loan_period']&&
doc['loan_amount'])
{ emit([doc['template_id'],
doc['loan_name'],doc['loan_period'],
doc['loan_amount']],null);}}"
I need to get the whole docs with loan_period > 5 and
loan_amount > 30000. My startkey and endkey parameters are like this:
params = {:startkey =>["7446567e45dc5155353736cb3d6041c0",nil,5,30000],
:endkey=>["7446567e45dc5155353736cb3d6041c0",{},{},{}],:include_docs => true}
Here, I am not getting the desired result. I think my startkey and endkey params are wrong. Can anyone help me?
A CouchDB view is an ordered list of entries. Queries on a view return a contiguous slice of that list. As such, it's not possible to apply two inequality conditions.
Assuming that your loan_period is a discrete variable, this case would probably be best solved by emit'ing the loan_period first and then issuing one query for each period.
An alternative solution would be to use couchdb-lucene.
You're using arrays as your keys. Couchdb will compare arrays by comparing each array element in increasing order until two element are not equal.
E.g. to compare [1,'a',5] and [1,'c',0] it will compare 1 whith 1, then 'a' with 'c' and will decide that [1,'a',5] is less than [1,'a',0]
This explains why your range key query fails:
["7446567e45dc5155353736cb3d6041c0",nil,5,30000] is greater ["7446567e45dc5155353736cb3d6041c0",nil,5,90000]
Your emit statement looks a little strange to me. The purpose of emit is to produce a key (i.e. an index) and then the document's values that you are interested in.
for example:
emit( doc.index, [doc.name, doc.address, ....] );
You are generating an array for the index and no data for the view.
Also, Couchdb doesn't provide for an intersection of views as it doesn't fit the map/reduce paradigm very well. So your needs boil down to trying to address the following:
Can I produce a unique index which I can then extract a particular range from? (using startkey & endkey)
Actually CouchDB allows views to have complex keys which are arrays of values as given in the question:
[template_id, loan_name, loan_period, loan_amount]
Have you tried
params = {:startkey =>["7446567e45dc5155353736cb3d6041c0",nil,5,30000],
:endkey=>["7446567e45dc5155353736cb3d6041c0",{}],:include_docs => true}
or perhaps
params = {:startkey =>["7446567e45dc5155353736cb3d6041c0","\u0000",5,30000],
:endkey=>["7446567e45dc5155353736cb3d6041c0","\u9999",{}],:include_docs => true}

Resources