his presentation provides an in-depth introduction to MongoDB, a widely used NoSQL database known for its scalability, flexibility, and high performance. We will explore the fundamental concepts of MongoDB, including document-based storage, collections, CRUD operations, indexing, and aggregation fra...
his presentation provides an in-depth introduction to MongoDB, a widely used NoSQL database known for its scalability, flexibility, and high performance. We will explore the fundamental concepts of MongoDB, including document-based storage, collections, CRUD operations, indexing, and aggregation framework.
Additionally, the presentation will compare MongoDB with traditional relational databases (SQL), highlighting its advantages and use cases in modern applications such as big data, real-time analytics, and cloud-based applications.
The session will also include a live demonstration of how to set up a MongoDB database, insert and retrieve data, and perform queries efficiently. By the end of this presentation, attendees will have a solid understanding of how MongoDB works and why it is a preferred choice for developers in modern applications.Along with that we have also covered some common queriees that can be used in the document
Size: 1.47 MB
Language: en
Added: Mar 03, 2025
Slides: 21 pages
Slide Content
By : Manik Gupta 22BCE1243
mongoDB. Document-Oriented Storage : MongoDB stores data in flexible, JSON-like documents, making it easier to work with complex data structures and unstructured data. Schema Flexibility : Unlike traditional relational databases, MongoDB doesn't require a predefined schema. Fields in a document can vary, allowing dynamic changes to data structures. Horizontal Scalability : MongoDB supports horizontal scaling through sharding, distributing data across multiple servers to handle large volumes of data and traffic. High Performance : With features like indexing, in-memory storage, and replication, MongoDB provides efficient read and write operations for high-performance applications. Rich Query Language : MongoDB offers a powerful query language for filtering, aggregating, and transforming data, along with support for geospatial queries and full-text search.
WORKING OF mongoDB
1. Database A database in MongoDB is a container for collections. Each MongoDB instance can have multiple databases, and each database is independent of the others. A database is created when you insert data or collections into it, and it can be deleted by dropping the database. 2. Collection A collection is a group of MongoDB documents, analogous to a table in relational databases. Collections do not enforce any schema, meaning documents within the same collection do not need to have the same structure (fields and data types). Collections can store documents of any type or format, and the data can evolve over time. Schema-less : No rigid structure or rules on how documents are stored. 3. Document A document in MongoDB is the basic unit of storage, similar to a row in a relational database. Documents are stored in a BSON (Binary JSON) format, which is an extension of JSON. BSON supports more data types, like binary data and ObjectId (for unique identification), which JSON does not support. A document is a set of key-value pairs (fields and values).
Creating a Database The use<database_name> command makes a new database if it doesn't exist else it uses the database.
Inserting a Document Into A Collection insertOne() db.<collection>.insertOne() insertMany() db.<collection>.insertMany( < document1 >, <document2> )
Data Types Supported By mongoDB MongoDB supports a variety of data types, including: String: A basic and common data type that can store international characters as UTF-8 BSON strings Numeric values: MongoDB supports a variety of numeric data types, including: Integer: Stores numeric values Double: Stores numeric numbers as 64-bit IEEE 754 floating point Date: Stores the current date or time as a 64-bit integer Timestamp: Represents time as a sequence of characters Object: Stores embedded documents as a series of nested documents Array: An array data type Binary data: A binary data type
Finding Documents Using the find Operator When given equality with an _id field, the find() command will return the specified document that matches the _id . Here's an example: Using the $in Operator Use the $in operator to select documents where the value of a field equals any value in the specified array. Here's an example:
$gt Use the $gt operator to match documents with a field greater than the given value. For example: 1 db.sales.find({ "items.price": { $gt: 50}}) $lt Use the $lt operator to match documents with a field less than the given value. For example: 1 db.sales.find({ "items.price": { $lt: 50}}) $lte Use the $lte operator to match documents with a field less than or equal to the given value. For example: 1 db.sales.find({ "customer.age": { $lte: 65}}) $gte Use the $gte operator to match documents with a field greater than or equal to the given value. For example: 1 db.sales.find({ "customer.age": { $gte: 65}})
Find a Document by Using Implicit $and Use implicit $and to select documents that match multiple expressions. For example: db.collection('inventory').find({ status: 'A', qty: { $lt: 30 } }); Find a Document by Using the $or Operator Use the $or operator to select documents that match at least one of the included expressions. For example: db.routes.find({ $or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }], })
Updating Documents $set The $set operator replaces the value of a field with the specified value, as shown in the following code: db.podcasts.updateOne({ _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8"), },{ $set: { subscribers: 98562, }, } )
$push The $push operator adds a new value to the hosts array field. Here's an example: db.podcasts.updateOne( { _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8") }, { $push: { hosts: "Nic Raboy" } } ) upsert The upsert option creates a new document if no documents match the filtered criteria. Here's an example: db.podcasts.updateOne( { title: "The Developer Hub" }, { $set: { topics: ["databases", "MongoDB"] } }, { upsert: true } )
Deleting Documents in mongoDB Delete One Document The following code shows an example of the deleteOne() method: db.podcasts.deleteOne({ _id: Objectid("6282c9862acb966e76bbf20a") }) Delete Many Documents The following code shows an example of the deleteMany() method: db.podcasts.deleteMany({category: “crime”})
Sorting and Limiting Query Results %sort: Use cursor.sort() to return query results in a specified order. Within the parentheses of sort() , include an object that specifies the field(s) to sort by and the order of the sort. Use 1 for ascending order, and -1 for descending order. Syntax: db.collection.find(<query>).sort(<sort>) Example // Return data on all music companies, sorted alphabetically from A to Z. Ensure consistent sort order db.companies.find({ category_code: "music" }).sort({ name: 1, _id: 1 });
Limiting Results Use cursor.limit() to return query results in a specified order. Within the parentheses of limit() , specify the maximum number of documents to return. Syntax: db.companies.find(<query>).limit(<number>) Example: db.companies .find({ category_code: "music" }) .sort({ number_of_employees: -1, _id: 1 }) .limit(3);
COUNT DOCUMENTS Use db.collection.countDocuments() to count the number of document s Syntax: db.collection.countDocuments( <query>, <options> ) // Count number of trips over 120 minutes by subscribers db.trips.countDocuments({ tripduration: { $gt: 120 }, usertype: "Subscriber" })