Ticker

6/recent/ticker-posts

MongoDB Documents

MongoDB Documents

Introduction to MongoDB Documents:

MongoDB is a popular NoSQL database that stores data in the form of JSON-like objects called documents. A document in MongoDB is the basic unit of data storage and is analogous to a row in a relational database. It consists of key-value pairs, where keys are strings (field names), and values can be various data types such as strings, numbers, arrays, sub-documents, and more.

Creating a Document:

To create a document in MongoDB, you interact with collections. Collections are groups of related documents that share a common purpose. Here's an example of creating a document using the MongoDB Shell:

javascript
// Syntax to insert a document into a collection
db.collection.insertOne(
{
key1: value1,
key2: value2,
// More key-value pairs
}
);

Example:

javascript
// Inserting a document into the "users" collection
db.users.insertOne(
{
name: "John Doe",
age: 30,
email: "john.doe@example.com",
address: {
city: "New York",
country: "USA"
},
hobbies: ["reading", "cooking", "swimming"]
}
);

Explanation:

In the example above, we created a document representing a user with various fields:

  • name: A string field storing the user's name.
  • age: A number field storing the user's age.
  • email: A string field storing the user's email address.
  • address: A sub-document (nested document) representing the user's address, with city and country fields.
  • hobbies: An array field containing the user's hobbies.

Querying a Document:

You can query documents in MongoDB to retrieve data based on specific criteria. The find method is used for querying:

javascript
// Syntax to query a document in a collection
db.collection.find({ key: value });

Example:

javascript
// Querying users with age greater than 25
db.users.find({ age: { $gt: 25 } });

Explanation:

In this example, we're querying the "users" collection to find documents where the "age" field is greater than 25.

Updating a Document:

To update a document in MongoDB, you use the updateOne or updateMany methods:

javascript
// Syntax to update a document in a collection
db.collection.updateOne(
{ filter },
{ $set: { key: value } }
);

Example:

javascript
// Updating the email address for a user named John Doe
db.users.updateOne(
{ name: "John Doe" },
{ $set: { email: "john.doe.updated@example.com" } }
);

Explanation:

In this example, we are updating the email address for the user document with the name "John Doe" using the $set operator.

Deleting a Document:

To delete a document from MongoDB, you can use the deleteOne or deleteMany methods:

javascript
// Syntax to delete a document from a collection
db.collection.deleteOne({ filter });

Example:

javascript
// Deleting a user document with a specific email address
db.users.deleteOne({ email: "john.doe@example.com" });

Explanation:

In this example, we are deleting a user document that matches the email address "john.doe@example.com".

Conclusion:

MongoDB documents are a fundamental part of data storage in MongoDB. They allow for flexible and schema-less data representation, making MongoDB an excellent choice for various types of applications, especially those with evolving data requirements. By understanding how to create, query, update, and delete documents, you can effectively work with MongoDB and build powerful applications that leverage its document-based data model.

    Post a Comment

    0 Comments