查找多个文档
您可以使用以下方式在集合中查询多个文档:collection.find()
。该find()
方法使用您提供的查询文档来匹配集合中与查询匹配的文档子集。如果您不提供查询文档(或提供空文档),MongoDB将返回集合中的所有文档。有关查询MongoDB的更多信息,请参阅我们的查询文档文档.
您还可以定义更多查询选项,例如 排序 和 投影 以配置结果集。您可以在find()
方法调用中的选项参数中指定这些选项,在sort
和projection
对象中。请参见collection.find()有关传递给方法的参数的更多信息。
find()
方法返回一个FindCursor来管理查询的结果。您可以使用for await...of
语法或以下以下游标方法之一遍历匹配的文档:
next()
toArray()
如果没有文档与查询匹配,find()
将返回一个空游标。
兼容性
您可以使用Node.js驱动程序来连接并使用 find()
方法在以下环境中托管的应用程序
MongoDB Atlas:MongoDB在云中的托管服务
MongoDB Enterprise:基于订阅的、自我管理的MongoDB版本
MongoDB Community:源代码可用的、免费使用并自我管理的MongoDB版本
要了解更多关于在Atlas UI中查找文档的信息有关在MongoDB Atlas中托管的应用程序,请参阅创建、查看、更新和删除文档。
示例
以下代码示例从movies
集合中查找文档。它使用以下参数
一个 查询文档,配置查询只返回运行时间小于15分钟的影片。
一个 排序,按标题升序组织返回的文档(按字母顺序,“A”在“Z”之前,“1”在“9”之前)。
一个 投影,显式排除返回文档中的
_id
字段,并显式包含仅包含title
和imdb
对象(及其嵌套字段)。
注意
您可以使用此示例连接到MongoDB的一个实例,并与之交互包含示例数据的数据库。要了解更多关于连接到您的MongoDB实例和加载数据集的信息,请参阅用法示例指南。
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 11 // Get the database and collection on which to run the operation 12 const database = client.db("sample_mflix"); 13 const movies = database.collection("movies"); 14 15 // Query for movies that have a runtime less than 15 minutes 16 const query = { runtime: { $lt: 15 } }; 17 18 const options = { 19 // Sort returned documents in ascending order by title (A->Z) 20 sort: { title: 1 }, 21 // Include only the `title` and `imdb` fields in each returned document 22 projection: { _id: 0, title: 1, imdb: 1 }, 23 }; 24 25 // Execute query 26 const cursor = movies.find(query, options); 27 28 // Print a message if no documents were found 29 if ((await movies.countDocuments(query)) === 0) { 30 console.log("No documents found!"); 31 } 32 33 // Print returned documents 34 for await (const doc of cursor) { 35 console.dir(doc); 36 } 37 38 } finally { 39 await client.close(); 40 } 41 } 42 run().catch(console.dir);
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 const client = new MongoClient(uri); 7 8 type Minutes = number; 9 10 interface IMDB { 11 rating: number; 12 votes: number; 13 id: number; 14 } 15 16 interface Movie { 17 title: string; 18 imdb: IMDB; 19 runtime: Minutes; 20 } 21 22 async function run() { 23 try { 24 const database = client.db("sample_mflix"); 25 const movies = database.collection<Movie>("movies"); 26 27 const query = { runtime: { $lt: 15 } }; 28 const cursor = movies.find<Movie>( 29 query, 30 { 31 sort: { title: 1 }, 32 projection: { _id: 0, title: 1, imdb: 1 }, 33 } 34 ); 35 36 if ((await movies.countDocuments(query)) === 0) { 37 console.warn("No documents found!"); 38 } 39 40 for await (const doc of cursor) { 41 console.dir(doc); 42 } 43 } finally { 44 await client.close(); 45 } 46 } 47 run().catch(console.dir);
运行前面的示例,您将看到以下输出
{ title: '10 Minutes', imdb: { rating: 7.9, votes: 743, id: 339976 } } { title: '3x3', imdb: { rating: 6.9, votes: 206, id: 1654725 } } { title: '7:35 in the Morning', imdb: { rating: 7.3, votes: 1555, id: 406501 } } { title: '8', imdb: { rating: 7.8, votes: 883, id: 1592502 } } ...
您可以将sort
和projection
选项也指定为方法(sort()
和project()
),这些方法链接到find()
方法。以下两个命令是等效的
movies.find({ runtime: { $lt: 15 } }, { sort: { title: 1 }, projection: { _id: 0, title: 1, imdb: 1 }}); movies.find({ runtime: { $lt: 15 } }).sort({ title: 1}).project({ _id: 0, title: 1, imdb: 1 });