How do I use the number of active users of my app with my Cloudant database?

21 Views Asked by At

I have some user documents in my Cloudant database that are updated every time a user logs in to our app. I'm trying to find how many users logged in within the past 24hrs. What is the best way of achieving this in Cloudant?

1

There are 1 best solutions below

0
Daniel Mermelstein On

Imagine your document looks like this:

{
"_id": "abc1234",
"user_id": "xyz567",
"last_login": "2022-11-23T12:00:00.000Z"
}

You can create a view keyed on the last_login parameter (and select the _count Reducer):

function (doc) {
  emit(doc.last_login);
}

If you then query that view and supply a startkey parameter with the date you want to compare to, you will get the count of users who have logged in since that date, e.g.

https://mycouchdb.com/userslogin/_design/reports/_view/logybytime?startkey="2022-11-23"

This will show the number of users who have logged in since November 23, 2022.