In this section, we will cover how to create documents in MongoDB. Documents are the basic unit of data in MongoDB, similar to rows in a relational database. Each document is a JSON-like object that can contain various fields and data types.
Key Concepts
- Document Structure: Documents in MongoDB are stored in collections. Each document is a JSON-like object.
- Inserting Documents: The primary method to insert documents into a collection is using the
insertOne
andinsertMany
methods. - Data Types: MongoDB supports various data types, including strings, numbers, arrays, and embedded documents.
Document Structure
A document in MongoDB is a JSON-like object. Here is an example of a simple document:
{ "name": "John Doe", "age": 29, "email": "[email protected]", "address": { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345" }, "hobbies": ["reading", "traveling", "swimming"] }
Inserting Documents
insertOne
Method
The insertOne
method is used to insert a single document into a collection.
Syntax:
Example:
db.users.insertOne({ "name": "John Doe", "age": 29, "email": "[email protected]", "address": { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345" }, "hobbies": ["reading", "traveling", "swimming"] })
insertMany
Method
The insertMany
method is used to insert multiple documents into a collection.
Syntax:
Example:
db.users.insertMany([ { "name": "Jane Smith", "age": 32, "email": "[email protected]", "address": { "street": "456 Elm St", "city": "Othertown", "state": "NY", "zip": "67890" }, "hobbies": ["cooking", "hiking"] }, { "name": "Mike Johnson", "age": 41, "email": "[email protected]", "address": { "street": "789 Oak St", "city": "Sometown", "state": "TX", "zip": "11223" }, "hobbies": ["fishing", "cycling"] } ])
Practical Exercises
Exercise 1: Insert a Single Document
Task:
Insert a single document into a collection named products
with the following fields:
name
: "Laptop"brand
: "Dell"price
: 1200stock
: 50
Solution:
Exercise 2: Insert Multiple Documents
Task:
Insert multiple documents into a collection named products
with the following fields:
name
: "Smartphone",brand
: "Apple",price
: 999,stock
: 30name
: "Tablet",brand
: "Samsung",price
: 600,stock
: 20
Solution:
db.products.insertMany([ { "name": "Smartphone", "brand": "Apple", "price": 999, "stock": 30 }, { "name": "Tablet", "brand": "Samsung", "price": 600, "stock": 20 } ])
Common Mistakes and Tips
- Missing Fields: Ensure that all required fields are included in the document.
- Data Types: Verify that the data types of the fields are correct. For example, the
price
field should be a number, not a string. - Unique Fields: If a field is supposed to be unique (e.g.,
email
), ensure that you handle duplicates appropriately.
Conclusion
In this section, we learned how to create documents in MongoDB using the insertOne
and insertMany
methods. We also covered the structure of a document and provided practical exercises to reinforce the concepts. In the next section, we will explore how to read documents from a MongoDB collection.