统计文档数量
Node.js 驱动程序为集合中的文档计数提供了两种方法
collection.countDocuments() 返回与指定查询匹配的集合中的文档数量。如果您指定一个空的查询文档,
countDocuments()
返回集合中的文档总数。collection.estimatedDocumentCount() 返回集合中文档数量的一个 估计,基于集合元数据。
estimatedDocumentCount()
比 countDocuments()
快,因为估计使用的是集合的元数据,而不是扫描集合。相比之下,countDocuments()
返回时间较长,但提供了文档数量的 准确 计数,并支持指定过滤器。根据您的负载选择适当的方法。
要指定要计数的文档,countDocuments()
接受一个查询 参数。 countDocuments()
计数匹配指定查询的文档。
countDocuments()
和 estimatedDocumentCount()
支持影响方法执行的可选设置。有关更多信息,请参阅每个方法的参考文档。
提示
使用countDocuments()
方法返回集合中文档的总数时,可以通过避免集合扫描来提高性能。为此,请使用提示来利用_id
字段的内置索引。仅在调用countDocuments()
时带有空查询参数时使用此技术。
collection.countDocuments({}, { hint: "_id_" });
示例
以下示例估计了sample_mflix
数据库中movies
集合中的文档数量,然后使用带有Canada
字段中Canada
的精确计数返回movies
集合中的文档数量。
注意
您可以使用此示例连接到MongoDB实例并交互包含示例数据的数据库。有关连接到MongoDB实例和加载示例数据集的更多信息,请参阅使用示例指南。
1 // Count documents in a collection 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async function run() { 11 try { 12 const database = client.db("sample_mflix"); 13 const movies = database.collection("movies"); 14 15 /* Print the estimate of the number of documents in the 16 "movies" collection */ 17 const estimate = await movies.estimatedDocumentCount(); 18 console.log(`Estimated number of documents in the movies collection: ${estimate}`); 19 20 /* Print the number of documents in the "movies" collection that 21 match the specified query */ 22 const query = { countries: "Canada" }; 23 const countCanada = await movies.countDocuments(query); 24 console.log(`Number of movies from Canada: ${countCanada}`); 25 } finally { 26 // Close the connection after the operations complete 27 await client.close(); 28 } 29 } 30 // Run the program and print any thrown exceptions 31 run().catch(console.dir);
1 // Count documents in a collection 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async function run() { 11 try { 12 const database = client.db("sample_mflix"); 13 const movies = database.collection("movies"); 14 15 /* Print the estimate of the number of documents in the 16 "movies" collection */ 17 const estimate = await movies.estimatedDocumentCount(); 18 console.log(`Estimated number of documents in the movies collection: ${estimate}`); 19 20 /* Print the number of documents in the "movies" collection that 21 match the specified query */ 22 const query = { countries: "Canada" }; 23 const countCanada = await movies.countDocuments(query); 24 console.log(`Number of movies from Canada: ${countCanada}`); 25 } finally { 26 // Close the connection after the operations complete 27 await client.close(); 28 } 29 } 30 // Run the program and print any thrown exceptions 31 run().catch(console.dir);
注意
相同的代码片段
上面的JavaScript和TypeScript代码片段相同。此用例中与TypeScript相关的驱动程序没有特定功能。
运行前面的示例代码,您将看到以下输出
Estimated number of documents in the movies collection: 23541 Number of movies from Canada: 1349