Populate the Students Collection

MongoDBMongoDBBeginner
Practice Now

Introduction

In this challenge, your task is to use MongoDB to populate a students collection with multiple documents. This will test your understanding of the insertMany command, which allows for the insertion of multiple documents into a collection in a single operation.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mongodb(("`MongoDB`")) -.-> mongodb/BasicOperationsGroup(["`Basic Operations`"]) mongodb/BasicOperationsGroup -.-> mongodb/create_database_collection("`Create Database and Collection`") mongodb/BasicOperationsGroup -.-> mongodb/insert_document("`Insert Document`") mongodb/BasicOperationsGroup -.-> mongodb/bulk_insert_documents("`Bulk Insert Documents`") subgraph Lab Skills mongodb/create_database_collection -.-> lab-425481{{"`Populate the Students Collection`"}} mongodb/insert_document -.-> lab-425481{{"`Populate the Students Collection`"}} mongodb/bulk_insert_documents -.-> lab-425481{{"`Populate the Students Collection`"}} end

Insert Multiple Documents into the students Collection

You will populate the students collection with the following records using the insertMany command.

Tasks

Insert the following documents into the students collection:

[
  { "name": "Alice Brown", "age": 20, "major": "Engineering", "gpa": 3.6 },
  { "name": "Bob White", "age": 22, "major": "Physics", "gpa": 3.8 },
  { "name": "Charlie Green", "age": 19, "major": "Mathematics", "gpa": 3.9 }
]

Requirements

  • Use the insertMany command in the MongoDB Shell (mongosh).
  • Operate within the university database.
  • Ensure all three documents are successfully inserted into the students collection.

Example

After completing the task, running the following command:

db.students.find()

Should output:

{
  "_id": ObjectId("..."),
  "name": "Alice Brown",
  "age": 20,
  "major": "Engineering",
  "gpa": 3.6
}
{
  "_id": ObjectId("..."),
  "name": "Bob White",
  "age": 22,
  "major": "Physics",
  "gpa": 3.8
}
{
  "_id": ObjectId("..."),
  "name": "Charlie Green",
  "age": 19,
  "major": "Mathematics",
  "gpa": 3.9
}
âœĻ Check Solution and Practice

Summary

In this challenge, you learned to use the insertMany command to efficiently populate a MongoDB collection with multiple documents. This skill is fundamental for managing data in MongoDB and sets the stage for more complex operations like querying and updating records.

Other MongoDB Tutorials you may like