0

I am pretty new to this map-reduce thing and I am using this to filter out users on my site.

What I want to ask is how can I do the group based filtering in it?

Let me explain you the scenario:- I want to filter out the queries ask by users between particular dates e.g> from:-1/01/2016 To:-03/02/2016.

I am getting the data given below by running a MR function I further want to run a Reduce function again over it on Cloudant so that i can get the queries made between particular dates

I am using Cloudant, Mongodb, Couchdb and JavaScript.

Thanks for replying and giving your precious time to read my query. Sorry for the trouble if you had in understanding query as I am really new to all this.

  • Please show us some sample data and specify in which of mentioned technologies would you like to use this query – Andrzej Smyk Oct 02 '17 at 07:36
  • Possible duplicate of [Find objects between two dates MongoDB](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) – Serge K. Oct 02 '17 at 07:38
  • @AndrzejSmyk I am using Cloudant as far now. See the main purpose is that i am trying to build a site or user interaction right and people ask queries there related to issues. Now I want to just make a page so that i get to know that between certain dates how much and related to what topic queries are asked. – noob_coder Oct 02 '17 at 10:33
  • The question is still not very clear as @AndrzejSmyk pointed out... Assuming that you'll have some sort of JSON document, you need to have the date as one of the fields and a view created to filter it out – Neeraj Krishna Oct 02 '17 at 16:16
  • @NeerajKrishna See the above comment for the data i am having i am getting this by running a MR function over my db now I want that if a user enters a start and end date that he needs queries between declared dates. the a MR should run on the given data and fetch the querries between these dates. Hope i am clear this time. – noob_coder Oct 03 '17 at 07:17

1 Answers1

0

If the dates in your query are dynamically provided at runtime then a view may not be the right approach. You can use Cloudant Query to run dynamic queries at runtime. For example, you can create an index on your date field like so:

{
  "index": {
    "fields": [
      "mydate"
    ]
  },
  "type": "json"
}

and then use the following selector in a Cloudant Query:

"selector": {
   "$and": [
      {"mydate" : { "$gt": 1506874127 }},
      {"mydate": { "$lt": 1506960651 }}
   ]
}

I am using unix timestamps in this example.

Alternatively, you can use Cloudant Search. Create a search index in Cloudant similar to the following:

{
  "_id": "_design/allDocs",
  "views": {},
  "language": "javascript",
  "indexes": {
    "byMyDate": {
      "analyzer": "standard",
      "index": "function (doc) {\n  if (doc.mydate) {\n    index(\"mydate\", doc.mydate);\n  }\n}"
    }
  }
}

This corresponds to the following when using the Cloudant dashboard:

design doc = allDocs

index name = byMyDate

index function =

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

Then you can run a search using a range. For example,

https://<YOUR_INSTANCE>.cloudant.com/<YOUR_DB>/_design/allDocs/_search/byMyDate?q=mydate%3A[1506874127%20TO%201506960651]

Here the search query is:

mydate:[1506874127 TO 1506960651]

Again, I am using unix timestamps. You could also use date strings I believe.

Cloudant Query: https://console.bluemix.net/docs/services/Cloudant/api/cloudant_query.html

Cloudant Search: https://console.bluemix.net/docs/services/Cloudant/api/search.html#search

markwatsonatx
  • 3,391
  • 2
  • 21
  • 19