user13695212
user13695212

Reputation:

How to count documents in mongodb/express

I have a express API and I wan't to return the documents that match the query params, as well as the number of documents.

I have the following query, and I wan't to return the following object.

  const property = await Property.find(query1);
  res.json({ 'results:': property.countDocuments(), property });

I get the following error

property.countDocuments is not a function

Upvotes: 0

Views: 1387

Answers (2)

Yusuf
Yusuf

Reputation: 2388

find returns an array of documents, so you can just use, .length property.length

Upvotes: 0

gaganshera
gaganshera

Reputation: 2639

Just do this:

const property = await Property.countDocuments(query1);

Upvotes: 1

Related Questions