Module 04 · NoSQL Databases in Cloud云端 NoSQL 数据库
Hands-on Lab: MongoDB CRUD动手实验:MongoDB 增删改查
预计所需时间:30分钟
目标
完成本实验后,您将能够:
- 使用插入方法在 MongoDB 中创建文档
- 通过列出、计数和匹配查询来读取文档
- 根据特定标准在 MongoDB 中更新和删除文档
关于技能网络云 IDE
技能网络云 IDE(基于 Theia 和 Docker)为课程和项目相关实验提供了一个实践环境。Theia 是一个开源的集成开发环境(IDE),可以在桌面或云端运行。为了完成这个实验,我们将使用技能网络提供的基于 Theia 和 MongoDB 的云 IDE。
关于此实验环境的重要通知
请注意,此实验环境的会话不会被保存。每次您连接到此实验时,都会为您创建一个新的环境。您在之前会话中保存的任何数据都将丢失。请计划在单个会话中完成这些实验,以避免丢失您的数据。
练习 1 - 准备环境
通过点击下面的按钮打开 MongoDB 数据库页面:
Open MongoDB Page in IDE
在该页面上,点击 Create 按钮以创建一个 MongoDB 数据库。
创建完成后,点击 MongoDB CLI 按钮(在上一步的 Create 按钮下方)以使用 mongosh CLI 连接到数据库。
- 问题:
选择 training 数据库。
点击这里获取提示
请参考 Mongodb 入门 实验。
点击这里获取解决方案
use training
- 问题:
创建一个名为 languages 的集合。
点击这里获取提示
请参考 Mongodb 入门 实验。
点击这里获取解决方案
db.createCollection("languages")
练习 2 - 插入文档
让我们将五个文档插入到集合 languages 中。
在 mongo 客户端运行以下命令。
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
练习 3 - 阅读文档
让我们尝试不同的查询文档的方法。
查找文档的数量。
db.languages.count()
列出集合中的第一个文档。
db.languages.findOne()
列出集合中的所有文档。
db.languages.find()
列出集合中的前三个文档。
db.languages.find().limit(3)
Query for “python” language.
db.languages.find({"name":"python"})
Query for “object oriented” languages.
db.languages.find({"type":"object oriented"})
List only specific fields.
Using a projection document you can specify what fields we wish to see or skip in the output.
This command lists all the documents with only name field in the output.
db.languages.find({},{"name":1})
This command lists all the documents without the name field in the output.
db.languages.find({},{"name":0})
This command lists all the “object oriented” languages with only “name” field in the output.
db.languages.find({"type":"object oriented"},{"name":1})
Exercise 4 - Update documents
Update documents based on a criteria.
Add a field to all the documents.
The ‘updateMany’ command is used to update documents in a mongodb collection, and it has the following generic syntax.
db.collection.updateMany({what documents to find},{$set:{what fields to set}})
Here we are adding a field description with value programming language to all the documents.
db.languages.updateMany({},{$set:{"description":"programming language"}})
Set the creater for python language.
db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
Set a field named compiled with a value true for all the object oriented languages.
db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":true}})
Exercise 5 - Delete documents
Delete documents based on a criteria.
Delete the scala language document.
db.languages.remove({"name":"scala"})
Delete the object oriented languages.
db.languages.remove({"type":"object oriented"})
Delete all the documents in a collection.
db.languages.remove({})
Practice exercises
Run the below code on mongo console. It will insert 5 documents, which will serve as sample data for the next steps.
use training
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
- Problem:
Insert an entry for ‘Haskell’ programming language which is of type ‘functional’.
Click here for Hint
use the command db.collection.insert()
Click here for Solution
On the mongo client run the below commands.
db.languages.insert({"name":"Haskell","type":"functional"})
- Problem:
Query for all functional languages.
Click here for Hint
use the command db.collection.insert() with suitable options
Click here for Solution
On the mongo client run the below commands.
db.languages.find({"type":"functional"})
- Problem:
Add ‘Bjarne Stroustrup’ as creator for c++.
Click here for Hint
use the command db.collection.insert() with suitable options
Click here for Solution
On the mongo client run the below commands.
db.languages.updateMany({"name":"c++"},{$set:{"creator":"Bjarne Stroustrup"}})
- Problem:
Delete all functional programming languages.
Click here for Hint
use the command db.collection.remove() with suitable options
Click here for Solution
On the mongo client run the below commands.
db.languages.remove({"type":"functional"})
- Problem:
Disconnect from the mongodb server.
Click here for Hint
Refer to the Mongodb Getting Started lab.
Click here for Solution
Run the below command on the terminal.
exit