How can I count occurrences of a specific user in a Notion database's people field using the API?

I’m working on a Notion API project to track team member assignments across different projects in our database.

I need to build a function that counts how many times a specific person appears in the people property across all records in a database. I want to use this for resource planning and workload distribution.

Here’s what I’ve tried so far:

async function countUserMentions(databaseId, personId) {
  const queryResult = await notion.databases.query({
    database_id: databaseId,
    filter: {
      "property": "Assignees",
      "people": {
        "contains": personId,
      },
    },
  });

  return Object.keys(queryResult).length;
}

The weird thing is that this always returns 4 no matter which user I check. I looked at the official documentation and it seems like the people filter might not work the way I expected.

Is there another approach to get the count of how many database entries mention a particular user? Maybe I need to fetch all records first and then filter them manually?

Any suggestions would be really helpful!

You’re counting the keys of the query result object instead of the actual results. The queryResult object has metadata properties like results, next_cursor, etc. - that’s why you’re always getting 4. Your filtering looks right, but you need to access the results array. Change your return statement to: javascript return queryResult.results.length; This’ll give you the count of records where that person appears in the Assignees field. The contains filter for people properties works exactly like you’d expect - it matches any record where that person is included, whether they’re the only assignee or one of many. I’ve used this same pattern for workload tracking in my projects and it works reliably. Just make sure you’re using the correct person ID format (usually starts with a UUID) rather than the person’s name or email.

Your counting logic is the problem, not the API filter. When you use Object.keys(queryResult).length, you’re counting the response object’s properties, not the actual records. The Notion API response always has the same structure - results, has_more, next_cursor, and type - that’s why you always get 4.

Your contains filter for people properties is correct. I’ve done similar stuff tracking developer assignments across sprint boards. Just change how you access the results:

return queryResult.results.length;

Watch out for pagination with large databases. The API only returns 100 records by default, so you might need to handle next_cursor for complete counts. Also double-check your personId matches Notion’s format - it should be the person’s UUID from their user object.

yeh, ur countin wrong. Object.keys(queryResult).length just counts the props of the response, not the real results lol. try queryResult.results.length instead. also, if ur db has over 100 records, remember to handle pagination or ur counts will be off. contains filter is fine for people fields tho.