Docs 菜单
Docs 主页
/ / /
Node.js
/ /

查找多个文档

您可以使用 collection.find()查询集合中的多个文档。 find()方法使用您提供的查询文档来匹配集合中与查询匹配的文档子集。如果未提供查询文档(或提供空文档),MongoDB 将返回集合中的所有文档。有关查询 MongoDB 的更多信息,请参阅有关查询文档的文档。

还可以定义其他查询选项,如 sortprojection,以配置结果集。可以在 sortprojection 对象中的 find() 方法调用的选项参数中指定这些内容。请参阅 collection.find(),进一步了解可以传递给该方法的参数。

find() 方法返回 findCursor ,管理您的查询结果。您可以使用 for await...of 语法或以下游标方法之一遍历匹配的文档:

  • next()

  • toArray()

如果没有与此查询匹配的文档,find() 则会返回一个空游标。

您可以使用 Node.js 驱动程序连接到以下环境中托管的部署并对其使用 find() 方法:

要了解有关在 Atlas 用户界面中为 MongoDB Atlas 托管的部署查找文档的更多信息,请参阅创建、查看、更新和删除文档

以下代码段查找 movies 集合中的文档。它使用以下参数:

  • 一份查询文档,将该查询配置为仅返回运行时少于 15 分钟的电影。

  • 一种按标题升序组织返回文档的排序(按字母顺序,"A" 在 "Z" 之前,"1" 在 "9" 之前)。

  • 一个投影,它会从返回的文档中显式排除 _id 字段并显式仅包含 titleimdb 对象(及其嵌入式字段)。

注意

可以使用此示例连接到 MongoDB 实例,并与包含样本数据的数据库进行交互。如需了解有关连接到 MongoDB 实例和加载样本数据集的更多信息,请参阅使用示例指南

1import { MongoClient } from "mongodb";
2
3// Replace the uri string with your MongoDB deployment's connection string.
4const uri = "<connection string uri>";
5
6const client = new MongoClient(uri);
7
8async 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}
42run().catch(console.dir);
1import { MongoClient } from "mongodb";
2
3// Replace the uri string with your MongoDB deployment's connection string.
4const uri = "<connection string uri>";
5
6const client = new MongoClient(uri);
7
8type Minutes = number;
9
10interface IMDB {
11 rating: number;
12 votes: number;
13 id: number;
14}
15
16interface Movie {
17 title: string;
18 imdb: IMDB;
19 runtime: Minutes;
20}
21
22async 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}
47run().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 } }
...

sortprojection 选项也可以指定为链式调用至 find() 方法的方法(分别为 sort()project())。以下两个命令是等效的:

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 });

后退

查找文档