Introducing MongoDB: Projection, limits and sorting
Agenda Database Types of Databases Types of NoSQL Databases + examples Projection in Mongo DB + Advantages & Disadvantages Limits in Mongo DB + Advantages & Disadvantages Sorting in Mongo DB + Advantages & Disadvantages
Database A database is a systematic or organized collection of related information that is stored in such a way that it can be easily accessed, retrieved, managed, and updated. Types of Databases: Hierarchical Databases objected oriented Database Operational Databases Relational Databases NoSQL Databases (non relational database) Cl oud database Personal Databases Centralized Databases
NoSQL Databases NoSQL is a type of database that does not use the traditional table-based structure of relational databases. Instead, it uses a more flexible schema-less design. This means that data can be stored in any format and there is no need to predefine the structure of the data beforehand.
Types NoSQL Databases Document–Oriented Database: Data are stored in documents. Key Value Stores: Every data element in the database is stored as a pair of keys and values. Column-Oriented Database: Data are stored in a column rather than rows. Graph-based Database: It focuses on storing and managing data using graph structures. Examples of NoSQL databases: Mongo DB, Graph, CouchDB, Apache HBase , Redis, Oracle, Amazon, etc.
Projection in Mongo DB Projection refers to the ability to retrieve only the specific fields from a document that meet the criteria of a query. In other words: selecting only necessary data rather than selecting the whole data of the document The Find() method is used in projection. db.your_collection_name.find ({},{" field_name ": 1})
Advantages & Disadvantages Of Projection Advantages Reduced Data Transfer: By fetching only the required fields, it reduces the amount of data transferred over the network Improve performance: By retrieving only the necessary fields, it enhances query performance as it reduces the amount of data that needs to be processed by the database engine Disadvantages Possible Impact on Readability : Overusing projection and excluding essential fields from the query result could potentially impact the readability and comprehensibility of the retrieved data. Potential Overhead in Maintenance: If there are frequent changes in the required fields for retrieval, managing and updating projection parameters across multiple queries might introduce maintenance overhead.
Limit in Mongo DB Limits refers to the practice of restricting the number of documents returned by a query db.filed_name.find ().limit( numberof records you want to limit)
Advantages & Disadvantages Of Limits Advantages Improved Performance: Limiting the amount of data retrieved reduces the workload on the database server and can improve query performance Enhanced Security and Privacy: Limiting the fields retrieved allows excluding sensitive or unnecessary information, enhancing data security and privacy by reducing the exposure of confidential data. Disadvantages Incomplete Data: Limiting data retrieval might result in incomplete information, especially if crucial fields or documents are excluded. Complex Querying Logic: Adding limitations into queries can sometimes complicate query logic, especially when combining multiple conditions or constraints
Sorting in Mongo DB S orting allows you to arrange query results in a specified order based on one or more fields in the documents. . The sort () method is used for sorting documents. db.table.find ().sort({ field_name : 1 }) : sort in ascending order db.table.find ().sort({ field_name : -1 }) : sort in descending order
Advantages & Disadvantages Of Limits Advantages Enhanced Data Presentation: Sorting allows data to be presented in a structured and organized manner, for facilitating better reading Streamlined Analysis and Reporting: Sorting enables streamlined data analysis and reporting by arranging information in a specific order Disadvantages Impact on Query Performance : Sorting can affect the performance of queries, especially if sorting is performed on non-indexed fields or when sorting requires extensive computations due to the complexity of the sort key. Complexity: Implementing sorting logic in queries can sometimes introduce complexity, especially when dealing with multiple fields or complex sorting requirements.
Advantages & Disadvantages Of Limits Advantages Enhanced Data Presentation: Sorting allows data to be presented in a structured and organized manner, for facilitating better reading Streamlined Analysis and Reporting: Sorting enables streamlined data analysis and reporting by arranging information in a specific order Disadvantages Impact on Query Performance : Sorting can affect the performance of queries, especially if sorting is performed on non-indexed fields or when sorting requires extensive computations due to the complexity of the sort key. Complexity: Implementing sorting logic in queries can sometimes introduce complexity, especially when dealing with multiple fields or complex sorting requirements.