0

How can I derived from only the document in the last XXX months (based on ticket's lastUpdateDateTime field's value). The list is still sorted by the number of occurrences (how often is used), in descending order.

Currently I can sort by the number of occurrences, how to retrieve the document with the time limit at the sametime

call the view like : ......?startkey=["UserId"]&endkey=["UserId",{}]&group=true

Map:

function(doc) {
        var usersLength = doc.users.length;
        for (var i = 0; i < usersLength ; i++) {
                emit([doc.users[i].UserId,doc.emailAddress,doc.serialNumber], 1);
            }        
        }

reduce:

function(keys, values, rereduce)
{
  return sum(values);
}

the results

{"rows":[
{"key":["3432950","dd@dd"],"value":8},
{"key":["3432950","aa@aa"],"value":8},
{"key":["3432950","bb@bb"],"value":1},
{"key":["3432950","cc@cc"],"value":1}
]}

call the view like : ......?startkey=["UserId"]&endkey=["UserId",{}]&group=true

Jamesjin
  • 371
  • 2
  • 6
  • 15
  • You would have to emit the lastUpdateDateTime in your view and execute a query on it using complex keys (eg : ?startkey["UserId","7monthago"]&endkey=["UserId","Today"] – Alexis Côté Jul 14 '17 at 00:23
  • Hi @Alexis Côté thanks a lot, you mean like this?: emit([doc.users[i].UserId,doc.emailAddress,doc.serialNumber, lastUpdate], 1); But I am not sure it The can still sorted by the number of occurrences like this results: the results {"rows":[ {"key":["3432950","dd@dd"],"value":8}, {"key":["3432950","aa@aa"],"value":7}, {"key":["3432950","bb@bb"],"value":6}, {"key":["3432950","cc@cc"],"value":5} ]} – Jamesjin Jul 14 '17 at 01:10
  • Oh I miseed that part. Then you either have to do two queries or process the first query data locally and filter it or use Mango – Alexis Côté Jul 14 '17 at 12:42
  • Hi @Alexis Côté, thanks for your suggestion, can you give me more detail on this solution? how can I do 2 queries or process the first query data locally and filter it – Jamesjin Jul 14 '17 at 13:04
  • Easiest one : Query the data of the last 7 month for a specific user(can be easily done with startkey. Then, locally group the result together to get the number of occurences – Alexis Côté Jul 14 '17 at 13:47
  • Hi Alexis Côté, I knew query the data of the last 7 month for a specific user, and then is there way we can use map/reduce that group the result together to get the number of occurences? it is helpful if you can explain me on this more detail information, thanks a lot – Jamesjin Jul 15 '17 at 01:30
  • If you want the number of occurence in the last 7 month, that's possible to use map/reduce as you did. Simply add the timestamp to your map/reduce and add the group level to 2 (to sum the values correctly). If you want to fetch the data for last 7 month and get the number of occurence globally in the database, that can't be done through map/reduce. You have to do local filtering. – Alexis Côté Jul 16 '17 at 21:02

0 Answers0