Hello there! This lesson covers an essential aspect of working with MongoDB: inserting documents into MongoDB collections. The ability to add data to your database is fundamental for any application. Let’s dive into MongoDB’s data insertion mechanisms and explore the structure of the documents you will be inserting.
MongoDB offers two primary methods to insert data into collections:
-
insertOne(): Inserts a single document into a collection.
1db.collection.insertOne(document)
-
insertMany(): Inserts multiple documents into a collection.
1db.collection.insertMany([document1, document2, ...])
Using the insertOne()
method, you can add a single document to a collection. Here is an example where we insert a single book into the books
collection:
JavaScript1use library_db 2 3db.books.insertOne({ 4 title: "1984", 5 author: { 6 name: "George Orwell", 7 nationality: "British" 8 }, 9 published_year: 1949, 10 genre: "Dystopian", 11 publisher: { 12 name: "Secker & Warburg", 13 location: "London" 14 } 15})
In this example, the command db.books.insertOne()
inserts the specified document into the books
collection.
To add multiple documents at once, you can use the insertMany()
method, which requires an array of documents as its parameter. Below is an example that inserts two books into the books
collection:
JavaScript1use library_db 2 3db.books.insertMany([ 4 { 5 title: "The Great Gatsby", 6 author: { 7 name: "F. Scott Fitzgerald", 8 nationality: "American" 9 }, 10 published_year: 1925, 11 genre: "Fiction", 12 publisher: { 13 name: "Scribner", 14 location: "New York" 15 } 16 }, 17 { 18 title: "Moby Dick", 19 author: { 20 name: "Herman Melville", 21 nationality: "American" 22 }, 23 published_year: 1851, 24 genre: "Adventure", 25 publisher: { 26 name: "Harper & Brothers", 27 location: "New York" 28 } 29 } 30])
In this example, the insertMany()
method is used to insert an array of document objects, where each object represents a book. This command adds both books to the books
collection. Note that this function accepts an array of documents, denoted by the square brackets []
.
A MongoDB document can be quite flexible in terms of what it can contain. Here are a few key aspects combined into a single example:
JSON1{ 2 // Simple Fields 3 "title": "1984", 4 "published_year": 1949, 5 6 // Nested Fields 7 "author": { 8 "name": "George Orwell", 9 "nationality": "British" 10 }, 11 12 // Arrays 13 "genres": ["Dystopian", "Political Fiction"], 14 15 // Embedded Documents with Deep Nesting 16 "publisher": { 17 "name": "Secker & Warburg", 18 "location": "London", 19 "contact": { 20 "email": "info@secker-warburg.co.uk", 21 "phone": "+44 20 1234 5678", 22 "address": { 23 "street": "20 Vauxhall Bridge Rd", 24 "city": "London", 25 "postcode": "SW1V 2SA" 26 } 27 } 28 } 29}
Using these structures, you can create documents that encapsulate a wide variety of data models, allowing you to efficiently represent and store your information in MongoDB. Note that nested structures can be deeply nested, providing a rich and hierarchical data model. We'll work on how to handle such complicated structures later in the course path.
In this lesson, we covered the basics of inserting data into MongoDB collections. You learned how to use the insertOne()
method to insert a single document, the insertMany()
method to insert multiple documents, and explored the structure of MongoDB documents. These operations are vital for populating your database with initial data or adding new records as your application evolves. Get ready to practice these concepts in the upcoming exercises!