介绍
在本实验中,你将学习如何编写基本的 MongoDB 查询,包括查找所有文档、执行精确匹配、使用查询运算符、选择特定字段以及格式化查询输出。你将首先使用 find()
方法从 MongoDB 集合中检索所有文档,然后继续学习更高级的技术,例如查询精确匹配、使用查询运算符以及自定义输出。通过本实验,你将掌握使用 MongoDB 数据库所需的基本技能。
在本实验中,你将学习如何编写基本的 MongoDB 查询,包括查找所有文档、执行精确匹配、使用查询运算符、选择特定字段以及格式化查询输出。你将首先使用 find()
方法从 MongoDB 集合中检索所有文档,然后继续学习更高级的技术,例如查询精确匹配、使用查询运算符以及自定义输出。通过本实验,你将掌握使用 MongoDB 数据库所需的基本技能。
在这一步中,你将学习如何使用 find()
方法从 MongoDB 集合中检索所有文档。这是使用 MongoDB 数据库的基本技能。
首先,启动 MongoDB shell 并创建一个包含一些文档的示例数据库:
mongosh
进入 MongoDB shell 后,创建一个新数据库并添加一些示例数据:
use bookstore
db.books.insertMany([
{ title: "Python Basics", author: "John Smith", year: 2022 },
{ title: "MongoDB Essentials", author: "Jane Doe", year: 2023 },
{ title: "Web Development", author: "Alice Johnson", year: 2021 }
])
现在,要查找 books
集合中的所有文档,使用不带任何参数的 find()
方法:
db.books.find();
你应该会看到类似以下的输出:
[
{
_id: ObjectId("..."),
title: 'Python Basics',
author: 'John Smith',
year: 2022
},
{
_id: ObjectId("..."),
title: 'MongoDB Essentials',
author: 'Jane Doe',
year: 2023
},
{
_id: ObjectId("..."),
title: 'Web Development',
author: 'Alice Johnson',
year: 2021
}
]
让我们分解一下发生了什么:
use bookstore
创建或切换到 bookstore
数据库db.books.insertMany()
向 books
集合中添加多个文档db.books.find()
检索集合中的所有文档如果你希望输出更易读,可以使用 pretty()
:
db.books.find().pretty();
这将使用换行和缩进格式化输出,使其更易于阅读。
在这一步中,你将学习如何在 MongoDB 查询中执行精确匹配。基于上一步的内容,我们将继续使用 bookstore
数据库来演示如何精确检索文档。
首先,确保你已进入 MongoDB shell 并正在使用 bookstore
数据库:
use bookstore
要查找与特定字段完全匹配的文档,你可以使用带有查询对象的 find()
方法。让我们通过精确的书名查找一本书:
db.books.find({ title: "Python Basics" });
此查询将仅返回书名完全匹配 "Python Basics" 的文档。你应该会看到类似以下的输出:
[
{
_id: ObjectId("..."),
title: 'Python Basics',
author: 'John Smith',
year: 2022
}
]
现在,让我们尝试对另一个字段(作者)进行精确匹配:
db.books.find({ author: "Jane Doe" });
你还可以结合多个精确匹配条件来缩小搜索范围:
db.books.find({
title: "MongoDB Essentials",
author: "Jane Doe"
});
此查询将返回同时精确匹配书名和作者的文档。
让我们再探索一个关于年份字段的示例:
db.books.find({ year: 2022 });
关于精确匹配的重要注意事项:
如果你想查看有多少文档匹配你的查询,可以使用 countDocuments()
:
db.books.countDocuments({ year: 2022 });
在这一步中,你将学习如何使用 MongoDB 查询运算符来执行更复杂和灵活的查询。查询运算符允许你创建比精确匹配更高级的过滤器。
首先,确保你已进入 MongoDB shell 并正在使用 bookstore
数据库:
use bookstore
让我们向集合中添加更多书籍,以演示不同的运算符:
db.books.insertMany([
{ title: "Advanced Python", author: "John Smith", year: 2020, price: 39.99 },
{
title: "Data Science Handbook",
author: "Jane Doe",
year: 2019,
price: 45.5
},
{
title: "Machine Learning Basics",
author: "Alice Johnson",
year: 2021,
price: 55.25
}
]);
现在,让我们探索一些常见的查询运算符:
$gt
) 和小于 ($lt
) 运算符:db.books.find({ year: { $gt: 2020 } });
查找 2020 年前出版的书籍:
db.books.find({ year: { $lt: 2020 } });
$gte
) 和小于或等于 ($lte
) 运算符:db.books.find({ year: { $gte: 2020 } });
$ne
) 运算符:db.books.find({ author: { $ne: "John Smith" } });
$in
) 运算符:db.books.find({ author: { $in: ["John Smith", "Jane Doe"] } });
$nin
) 运算符:db.books.find({ author: { $nin: ["John Smith", "Jane Doe"] } });
db.books.find({
year: { $gte: 2020 },
price: { $lt: 50 }
});
此查询查找 2020 年或之后出版且价格低于 50 的书籍。
在这一步中,你将学习如何从 MongoDB 文档中选择和投影特定字段。当你只需要检索所需信息时,这非常有用,可以减少数据传输并提高查询性能。
首先,确保你已进入 MongoDB shell 并正在使用 bookstore
数据库:
use bookstore
find()
方法的第二个参数中指定:db.books.find({}, { title: 1, author: 1, _id: 0 });
分解查询:
{}
表示无过滤条件(选择所有文档)1
表示包含该字段_id: 0
显式排除默认的 _id
字段你应该会看到类似以下的输出:
[
{ title: 'Python Basics', author: 'John Smith' },
{ title: 'MongoDB Essentials', author: 'Jane Doe' },
...
]
db.books.find({ year: { $gt: 2020 } }, { title: 1, year: 1, _id: 0 });
此查询查找 2020 年后出版的书籍,并仅返回它们的书名和年份。
db.books.find({}, { price: 0 });
这将返回除 price
字段外的所有字段。
// 假设文档结构更复杂
db.books.find({}, { "author.name": 1, title: 1, _id: 0 });
重要注意事项:
_id
)_id
,除非显式设置为 0在这一步中,你将学习如何格式化和操作 MongoDB 查询的输出,使结果更具可读性和实用性。
首先,确保你已进入 MongoDB shell 并正在使用 bookstore
数据库:
use bookstore
.pretty()
以缩进格式输出结果:db.books.find().pretty();
.limit()
限制返回的文档数量:db.books.find().limit(2);
.sort()
对文档进行排序。参数是一个指定字段和排序方向的对象:// 按年份升序排序
db.books.find().sort({ year: 1 });
// 按年份降序排序
db.books.find().sort({ year: -1 });
db.books
.find()
.sort({ year: -1 }) // 按年份降序排序
.limit(3) // 获取前 3 条结果
.pretty(); // 格式化输出
.skip()
跳过指定数量的文档:// 跳过前 2 条文档
db.books.find().skip(2).limit(2);
.count()
或 countDocuments()
获取匹配文档的总数:// 统计所有书籍数量
db.books.countDocuments();
// 统计 2020 年后出版的书籍数量
db.books.countDocuments({ year: { $gt: 2020 } });
实际示例:
让我们结合多种格式化技术:
db.books
.find({ year: { $gte: 2020 } })
.sort({ price: 1 }) // 按价格升序排序
.limit(2) // 获取 2020 年后最便宜的 2 本书
.pretty();
在本实验中,你将学习如何执行基本的 MongoDB 查询,包括查找集合中的所有文档、查询精确匹配、使用查询运算符、选择特定字段以及格式化查询输出。你将首先学习如何使用 find()
方法从 MongoDB 集合中检索所有文档,这是使用 MongoDB 数据库的基本技能。然后,你将探索如何在 MongoDB 查询中执行精确匹配,从而检索具有特定字段值的文档。在整个实验中,你将深入理解 MongoDB 查询的基本技术。