Dataview: Count number of pages with specific key?

I just set up a bookshelf with dataview and want to add some stats about my library.

I used the following query to get the total number of books in my library:

`$= dv.pages('"Books/Library"').length`

Now I would like to get the number of books I have read this year.

My YAML frontmatter for each book looks like this:


So I’m looking for something along the lines of

Status = "read" and Date-Read >= date(2022-01-01) AND Date-Read <=date(2022-12-31)

and get a number as output.

Is this possible with the .lenght property?

Thank you for your help!

I’m better in dql (my knowledge in dvjs is almost zero), but try this:

`$= dv.pages('"Books/Library"').where(p => p.Status == "read" && p["Date-Read"] >= moment('2022-01-01') && p["Date-Read"] <= moment('2022-12-31')).length`

Thank you! Workes like a charm.
Glad I asked here, I would have never figured this out by myself.

Follow up bonus question:
Could I somehow use this query for a progress bar with progress value?
Say I want to read 30 books this year, the progress bar would show how far along I am with this goal.

Thanks again!

About progress bar, I only know to do that in DQL query. My knowledge in JS is almost zero. But I can try adapt this query (certainly not the best elegant solution…).
For example, if the goal is 30 books:

`$="<progress value='" + (dv.pages('"dv.tests/sort"').where(p => p.Status == "read" && p["Date-Read"] >= moment('2022-01-01') && p["Date-Read"] <= moment('2022-12-31')).length) / 30 * 100 + "' max='100'></progress>" + "<br>" + (dv.pages('"dv.tests/sort"').where(p => p.Status == "read" && p["Date-Read"] >= moment('2022-01-01') && p["Date-Read"] <= moment('2022-12-31')).length) + " / 30"`

Absolutely perfect. Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.