Update MongoDB Records

# Introduction In this lab, you will learn how to update MongoDB records using various techniques, including updating a single document, performing bulk updates, and utilizing update operators. You will also explore how to handle update results and verify the updated data. This lab covers the fundamental CRUD (Create, Read, Update, Delete) operations in MongoDB, equipping you with the necessary skills to effectively manage your data in a MongoDB database. The lab starts by demonstrating how to update a single document using the `updateOne()` method, followed by exploring bulk updates with the `updateMany()` method. You will then dive into using update operators, such as `$set`, to modify specific fields within documents. Additionally, you will learn how to handle the update results and verify the updated data to ensure the integrity of your MongoDB records. ## Setup ```bash #!/bin/bash # Exit on any error set -e # Ensure project directory exists mkdir -p ~/project # Install MongoDB Shell wget -qO- https://www.mongodb.org/static/pgp/server-8.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-8.0.asc echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list sudo apt-get update -y sudo apt-get install -y mongodb-mongosh # Start MongoDB service sudo service mongodb start # Wait for MongoDB to start sleep 5 # Prepare MongoDB database and collection mongosh << EOF use mylab_database db.books.drop() db.books.insertMany([ { title: "JavaScript Fundamentals", author: "Mike Johnson", year: 2022, pages: 350 }, { title: "Python Deep Dive", author: "Sarah Williams", year: 2021, pages: 450 }, { title: "Machine Learning Basics", author: "John Doe", year: 2020, price: 39.99 } ]) EOF echo "MongoDB Lab environment setup complete!" ```

|
60 : 00

Click the virtual machine below to start practicing