简体   繁体   中英

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

Just do this:

const property = await Property.countDocuments(query1);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM