From ccb5d9339ac13fb89155d641d3b90436e2d3307f Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 17 Jun 2024 08:31:26 +0530 Subject: [PATCH 1/2] mongo-docs-advanced-topic-docs-7-to-12 added --- .../Advanced MongoDB/mongodb-ObjectId.md | 69 +++++++++++ .../mongodb-indexing-limitations.md | 103 +++++++++++++++++ .../Advanced MongoDB/mongodb-map-reduce.md | 108 ++++++++++++++++++ .../mongodb-regular-expression.md | 65 +++++++++++ .../Advanced MongoDB/mongodb-text-search.md | 86 ++++++++++++++ .../working-with-rockmorgo.md | 69 +++++++++++ 6 files changed, 500 insertions(+) create mode 100644 docs/MongoDB/Advanced MongoDB/mongodb-ObjectId.md create mode 100644 docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md create mode 100644 docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md create mode 100644 docs/MongoDB/Advanced MongoDB/mongodb-regular-expression.md create mode 100644 docs/MongoDB/Advanced MongoDB/mongodb-text-search.md create mode 100644 docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-ObjectId.md b/docs/MongoDB/Advanced MongoDB/mongodb-ObjectId.md new file mode 100644 index 000000000..4957ddf28 --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/mongodb-ObjectId.md @@ -0,0 +1,69 @@ +--- +id: mongodb-ObjectId +title: MongoDB - ObjectId +sidebar_label: ObjectId +sidebar_position: 8 +tags: [mongodb, ObjectId, document identifier] +description: Understand the structure and usage of ObjectId in MongoDB, including creation, timestamp extraction, and conversion to string format. +--- + +We have been using MongoDB ObjectId in all the previous chapters. In this chapter, let's delve into the structure and usage of ObjectId. + +## ObjectId Structure + +An ObjectId is a 12-byte BSON type structured as follows: + +- The first 4 bytes represent the seconds since the Unix epoch. +- The next 3 bytes are the machine identifier. +- The following 2 bytes consist of the process id. +- The last 3 bytes are a random counter value. + +MongoDB utilizes ObjectIds as the default value of the `_id` field of each document, generated during document creation. This intricate combination ensures that all `_id` fields are unique. + +## Creating New ObjectId + +To generate a new ObjectId, use the following code snippet: + +```javascript +newObjectId = ObjectId() +``` + +The above statement will return a uniquely generated id like: + +``` +ObjectId("5349b4ddd2781d08c09890f3") +``` + +Alternatively, you can provide a 12-byte id explicitly: + +```javascript +myObjectId = ObjectId("5349b4ddd2781d08c09890f4") +``` + +## Creating Timestamp of a Document + +As the `_id` ObjectId inherently stores the 4-byte timestamp, you typically don't need to store the creation time of any document separately. You can retrieve the creation time of a document using the `getTimestamp` method: + +```javascript +ObjectId("5349b4ddd2781d08c09890f4").getTimestamp() +``` + +This will return the creation time of the document in ISO date format: + +``` +ISODate("2014-04-12T21:49:17Z") +``` + +## Converting ObjectId to String + +In certain scenarios, you may require the ObjectId value in a string format. To convert the ObjectId to a string, use the following code: + +```javascript +newObjectId.str +``` + +The above code will provide the string format of the ObjectId, such as: + +``` +5349b4ddd2781d08c09890f3 +``` diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md b/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md new file mode 100644 index 000000000..171022ebf --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md @@ -0,0 +1,103 @@ +--- +id: mongodb-indexing-limitations +title: MongoDB - Indexing Limitations +sidebar_label: Indexing Limitations +sidebar_position: 7 +tags: [mongodb, indexing, limitations, performance] +description: Understand the limitations of indexing in MongoDB, including overhead, RAM usage, query constraints, and key limits. +--- + +# MongoDB - Indexing Limitations + +In this chapter, we will learn about Indexing Limitations and its other components. + +## Extra Overhead + +Every index occupies some space as well as causes an overhead on each insert, update and delete. So if you rarely use your collection for read operations, it makes sense not to use indexes. + +> **Note:** +> - Indexes require additional space. +> - Each insert, update, and delete operation requires additional processing due to index maintenance. + +```mermaid +graph TD; + A[Insert Operation] --> B[Index Overhead]; + C[Update Operation] --> B; + D[Delete Operation] --> B; +``` + +## RAM Usage + +Since indexes are stored in RAM, you should make sure that the total size of the index does not exceed the RAM limit. If the total size increases the RAM size, it will start deleting some indexes, causing performance loss. + +> **Note:** Ensure that the total size of all indexes does not exceed the available RAM to avoid performance issues. + +```mermaid +graph TD; + A[Indexes] -->|Stored in| B[RAM]; + B -->|Exceeds| C[Performance Loss]; + C --> D[Deletes Some Indexes]; +``` + +## Query Limitations + +Indexing can't be used in queries which use: +- Regular expressions or negation operators like `$nin`, `$not`, etc. +- Arithmetic operators like `$mod`, etc. +- `$where` clause + +> **Note:** +> - Regular expressions and negation operators cannot use indexes. +> - Arithmetic operators and `$where` clause cannot use indexes. + +| Query Type | Index Usage | +|-----------------|--------------| +| Regular Expressions | Not Supported | +| Negation Operators | Not Supported | +| Arithmetic Operators | Not Supported | +| `$where` Clause | Not Supported | + +## Index Key Limits + +Starting from version 2.6, MongoDB will not create an index if the value of existing index field exceeds the index key limit. + +> **Note:** Index creation is restricted if any field value exceeds the index key limit. + +```mermaid +graph TD; + A[Index Creation] -->|Value exceeds limit| B[Not Created]; +``` + +## Inserting Documents Exceeding Index Key Limit + +MongoDB will not insert any document into an indexed collection if the indexed field value of this document exceeds the index key limit. The same is the case with `mongorestore` and `mongoimport` utilities. + +> **Note:** Documents with indexed field values exceeding the key limit will not be inserted into an indexed collection. + +```mermaid +graph TD; + A[Document Insertion] -->|Exceeds Key Limit| B[Insertion Failed]; + C[mongorestore] -->|Exceeds Key Limit| B; + D[mongoimport] -->|Exceeds Key Limit| B; +``` + +## Maximum Ranges + +- A collection cannot have more than 64 indexes. +- The length of the index name cannot be longer than 125 characters. +- A compound index can have a maximum of 31 fields indexed. + +> **Table: Maximum Ranges** + +| Limit Type | Maximum Value | +|--------------------------|---------------------| +| Number of Indexes | 64 | +| Length of Index Name | 125 Characters | +| Fields in Compound Index | 31 Fields | + +```mermaid +graph TD; + A[Maximum Ranges] --> B[64 Indexes per Collection]; + A --> C[125 Characters Index Name]; + A --> D[31 Fields in Compound Index]; +``` diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md b/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md new file mode 100644 index 000000000..73fa2d041 --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md @@ -0,0 +1,108 @@ +--- +id: mongodb-map-reduce +title: MongoDB - Map Reduce +sidebar_label: Map Reduce +sidebar_position: 9 +tags: [mongodb, map reduce, data processing] +description: Understand the MapReduce paradigm in MongoDB for condensing large volumes of data into useful aggregated results. +--- + +According to MongoDB documentation, MapReduce is a data processing paradigm used to condense large data sets into useful aggregated results. MongoDB employs the `mapReduce` command for map-reduce operations, primarily for processing substantial data sets. + +## MapReduce Command Syntax + +The basic syntax of the `mapReduce` command in MongoDB is as follows: + +```javascript +db.collection.mapReduce( + function() {emit(key,value);}, // map function + function(key,values) {return reduceFunction;}, // reduce function + { // options + out: collection, // specifies the location of the query result + query: document, // optional selection criteria for documents + sort: document, // optional sort criteria + limit: number // optional maximum number of documents to return + } +) +``` + +In the above syntax: + +- `map` is a JavaScript function that maps a value with a key and emits a key-value pair. +- `reduce` is a JavaScript function that reduces or groups documents with the same key. +- `out` specifies the location of the map-reduce query result. +- `query` specifies optional selection criteria for selecting documents. +- `sort` specifies optional sort criteria. +- `limit` specifies the optional maximum number of documents to return. + +### Using MapReduce + +Consider a document structure storing user posts with fields like `post_text`, `user_name`, and `status`. We will perform a mapReduce function to select active posts, group them by `user_name`, and count the number of posts by each user. + +```javascript +db.posts.mapReduce( + function() { emit(this.user_name, 1); }, + function(key, values) { return Array.sum(values); }, + { + query: { status: "active" }, + out: "post_total" + } +) +``` + +The above mapReduce query outputs the following result: + +```json +{ + "result" : "post_total", + "timeMillis" : 9, + "counts" : { + "input" : 4, + "emit" : 4, + "reduce" : 2, + "output" : 2 + }, + "ok" : 1 +} +``` + +To see the result of this mapReduce query, you can use the `find` operator: + +```javascript +db.post_total.find() +``` + +This query will return results showing the count of active posts for each user: + +```json +{ "_id" : "tom", "value" : 2 } +{ "_id" : "mark", "value" : 2 } +``` + +MapReduce queries in MongoDB are powerful for constructing complex aggregation queries using custom JavaScript functions, providing flexibility in data processing and analysis. + +### Diagram: MapReduce Process + +```mermaid +graph TD; + A[Query Collection] --> B(Map Function); + B --> C(Emit Key-Value Pairs); + C --> D(Reduce Function); + D --> E(Aggregated Results); +``` + +### Note: Performance Considerations + +When using MapReduce in MongoDB: +- Consider the performance overhead, especially for large data sets. +- Optimize your map and reduce functions for efficiency. +- Utilize indexes to improve query performance. + +### Table: MapReduce Options + +| Option | Description | +|--------|------------------------------------------------| +| out | Specifies the location of the query result. | +| query | Optional selection criteria for documents. | +| sort | Optional sort criteria. | +| limit | Optional maximum number of documents to return| diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-regular-expression.md b/docs/MongoDB/Advanced MongoDB/mongodb-regular-expression.md new file mode 100644 index 000000000..56f2e9bcb --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/mongodb-regular-expression.md @@ -0,0 +1,65 @@ +--- +id: mongodb-regular-expression +title: MongoDB - Regular Expression +sidebar_label: Regular Expression +sidebar_position: 11 +tags: [mongodb, regular expression, pattern matching] +description: Learn how to use regular expressions for pattern matching in MongoDB using the $regex operator. +--- + +Regular Expressions are commonly used in programming languages to search for patterns or words within strings. MongoDB supports regular expressions for string pattern matching using the $regex operator, based on PCRE (Perl Compatible Regular Expression) syntax. + +## Using Regular Expressions + +Assume we have a document in the `posts` collection: + +```javascript +db.posts.insert({ + "post_text": "enjoy the mongodb articles on tutorialspoint", + "tags": ["mongodb", "tutorialspoint"] +}) +``` + +To search for posts containing the string "tutorialspoint" in their `post_text` field using a regular expression: + +```javascript +db.posts.find({ post_text: { $regex: "tutorialspoint" } }).pretty() +``` + +### Diagram: Regular Expression Query Process + +```mermaid +graph LR; + A[Query Collection] --> B(Regex Pattern Matching); + B --> C(Retrieve Matching Documents); +``` + +### Note: Case Insensitive Search + +To perform a case-insensitive search, use the $options parameter with value "$i" in the regex query: + +```javascript +db.posts.find({ post_text: { $regex: "tutorialspoint", $options: "$i" } }) +``` + +### Using Regex for Array Elements + +Regular expressions can also be applied to array fields, such as searching for posts with tags starting from "tutorial": + +```javascript +db.posts.find({ tags: { $regex: "tutorial" } }) +``` + +### Optimizing Regex Queries + +- Indexed fields improve the performance of regular expression queries by using indexed values for matching. +- Prefix expressions in regular expressions, like "^tut," optimize searches by matching strings starting with specific characters. + +## Table: Regex Search Options + +| Option | Description | +|--------------|----------------------------------------------------------------| +| $regex | Operator for regular expression pattern matching. | +| $options | Optional parameter for regex options like case insensitivity. | +| ^ | Anchor for matching the start of a string. | +| $ | Anchor for matching the end of a string. | diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md b/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md new file mode 100644 index 000000000..32e809306 --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md @@ -0,0 +1,86 @@ +--- +id: mongodb-text-search +title: MongoDB - Text Search +sidebar_label: Text Search +sidebar_position: 10 +tags: [mongodb, text search, indexing] +description: Learn how to perform text search in MongoDB using text indexes to search inside string content efficiently. +--- + +Starting from version 2.4, MongoDB supports text indexes for searching inside string content. Text Search uses stemming techniques to search for specified words in string fields by dropping stemming stop words like "a," "an," "the," etc. MongoDB currently supports around 15 languages for text search. + +## Enabling Text Search + +Initially, Text Search was an experimental feature, but starting from version 2.6, it is enabled by default in MongoDB configurations. + +## Creating Text Index + +Consider the following documents in the `posts` collection: + +```javascript +db.posts.insert({ + "post_text": "enjoy the mongodb articles on tutorialspoint", + "tags": ["mongodb", "tutorialspoint"] +}) + +db.posts.insert({ + "post_text": "writing tutorials on mongodb", + "tags": ["mongodb", "tutorial"] +}) +``` + +To create a text index on the `post_text` field, enabling text search inside the posts' text: + +```javascript +db.posts.createIndex({ post_text: "text" }) +``` + +### Using Text Index + +After creating the text index on the `post_text` field, you can search for posts containing specific words using the `$text` operator: + +```javascript +db.posts.find({ $text: { $search: "tutorialspoint" } }).pretty() +``` + +This command retrieves documents that contain the word "tutorialspoint" in their post text. + +### Diagram: Text Search Process + +```mermaid +graph LR; + A[Query Collection] --> B(Text Indexing); + B --> C(Search using $text operator); + C --> D(Retrieve Matching Documents); +``` + +### Note: Performance Considerations + +When using text search in MongoDB: +- Text indexes can significantly improve search performance for string content. +- Consider the language-specific settings for stemming and stop words. +- Optimize text indexes based on the search requirements. + +### Table: Text Search Options + +| Option | Description | +|--------|--------------------------------------------------| +| $text | Operator for text search using text indexes. | +| $search| Specifies the search term or phrase. | +| $language| Optional parameter for specifying the language for text search. | + +## Deleting Text Index + +To delete an existing text index, first, find the name of the index using the `getIndexes` method: + +```javascript +db.posts.getIndexes() +``` + +After obtaining the index name, use the `dropIndex` method to delete the text index: + +```javascript +db.posts.dropIndex("post_text_text") +``` + +This command deletes the text index named "post_text_text" from the `posts` collection. diff --git a/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md b/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md new file mode 100644 index 000000000..696cb6ab0 --- /dev/null +++ b/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md @@ -0,0 +1,69 @@ +--- +id: working-with-rockmorgo +title: Working with RockMongo +sidebar_label: Working with RockMongo +sidebar_position: 12 +tags: [mongodb, RockMongo, administration, database management] +description: Learn how to work with RockMongo, a MongoDB administration tool, for managing databases, collections, documents, and more. +--- + +RockMongo is a powerful MongoDB administration tool that provides a user-friendly interface for managing MongoDB databases, collections, documents, and indexes. It is similar to PHPMyAdmin for PHP and MySQL. + +## Downloading and Installing RockMongo + +You can download the latest version of RockMongo from the [official GitHub repository](https://github.com/iwind/rockmongo). After downloading, follow these steps to install RockMongo: + +1. Unzip the downloaded package into your server's root folder. +2. Rename the extracted folder to "rockmongo." +3. Access RockMongo by opening any web browser and navigating to the `index.php` page in the `rockmongo` folder. +4. Log in using the default credentials: Username - admin, Password - admin. + +### Diagram: RockMongo Installation Process + +```mermaid +graph LR; + A[Download RockMongo] --> B(Unzip Package); + B --> C(Rename Folder to RockMongo); + C --> D(Access index.php in Browser); + D --> E(Login with Default Credentials); +``` + +### Note: Default Credentials + +Upon installation, RockMongo uses default credentials (Username: admin, Password: admin). Change these credentials for security purposes. + +## Basic Operations with RockMongo + +Let's explore some basic operations you can perform with RockMongo: + +### Creating New Database + +1. Click on the "Databases" tab. +2. Click "Create New Database." +3. Enter the new database name and click "Create." + +### Creating New Collection + +1. Select the desired database from the left panel. +2. Click "New Collection" at the top. +3. Provide the collection name and click "Create." + +### Creating New Document + +1. Navigate to the collection where you want to add documents. +2. Click "Insert" at the top. +3. Enter the document's data in JSON or array format and click "Save." + +### Export/Import Data + +1. Select a collection. +2. Click "Export/Import" at the top to export or import data in zip format. + +## Table: RockMongo Operations Overview + +| Operation | Description | +|---------------------|---------------------------------------------------------| +| Create New Database | Add a new database to the MongoDB server. | +| Create New Collection| Create a new collection within a database. | +| Create New Document | Add a new document to a collection. | +| Export/Import Data | Transfer data in and out of collections in zip format. | From 7a8a1f803cee8d8c13e5d25a4b5ab3f2543a5cc5 Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 17 Jun 2024 08:43:12 +0530 Subject: [PATCH 2/2] note updated --- .../mongodb-indexing-limitations.md | 22 ++++++++++++------- .../Advanced MongoDB/mongodb-map-reduce.md | 3 ++- .../Advanced MongoDB/mongodb-text-search.md | 4 ++-- .../working-with-rockmorgo.md | 3 ++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md b/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md index 171022ebf..232eef715 100644 --- a/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md +++ b/docs/MongoDB/Advanced MongoDB/mongodb-indexing-limitations.md @@ -15,9 +15,10 @@ In this chapter, we will learn about Indexing Limitations and its other componen Every index occupies some space as well as causes an overhead on each insert, update and delete. So if you rarely use your collection for read operations, it makes sense not to use indexes. -> **Note:** -> - Indexes require additional space. -> - Each insert, update, and delete operation requires additional processing due to index maintenance. +:::note +- Indexes require additional space. +- Each insert, update, and delete operation requires additional processing due to index maintenance. +::: ```mermaid graph TD; @@ -30,7 +31,9 @@ graph TD; Since indexes are stored in RAM, you should make sure that the total size of the index does not exceed the RAM limit. If the total size increases the RAM size, it will start deleting some indexes, causing performance loss. -> **Note:** Ensure that the total size of all indexes does not exceed the available RAM to avoid performance issues. +:::note + Ensure that the total size of all indexes does not exceed the available RAM to avoid performance issues. +::: ```mermaid graph TD; @@ -46,9 +49,10 @@ Indexing can't be used in queries which use: - Arithmetic operators like `$mod`, etc. - `$where` clause -> **Note:** -> - Regular expressions and negation operators cannot use indexes. -> - Arithmetic operators and `$where` clause cannot use indexes. +:::note +- Regular expressions and negation operators cannot use indexes. +- Arithmetic operators and `$where` clause cannot use indexes. +::: | Query Type | Index Usage | |-----------------|--------------| @@ -72,7 +76,9 @@ graph TD; MongoDB will not insert any document into an indexed collection if the indexed field value of this document exceeds the index key limit. The same is the case with `mongorestore` and `mongoimport` utilities. -> **Note:** Documents with indexed field values exceeding the key limit will not be inserted into an indexed collection. +:::note + Documents with indexed field values exceeding the key limit will not be inserted into an indexed collection. +::: ```mermaid graph TD; diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md b/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md index 73fa2d041..b026fdd61 100644 --- a/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md +++ b/docs/MongoDB/Advanced MongoDB/mongodb-map-reduce.md @@ -91,12 +91,13 @@ graph TD; D --> E(Aggregated Results); ``` -### Note: Performance Considerations +:::note Performance Considerations When using MapReduce in MongoDB: - Consider the performance overhead, especially for large data sets. - Optimize your map and reduce functions for efficiency. - Utilize indexes to improve query performance. +::: ### Table: MapReduce Options diff --git a/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md b/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md index 32e809306..39dd3636d 100644 --- a/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md +++ b/docs/MongoDB/Advanced MongoDB/mongodb-text-search.md @@ -54,13 +54,13 @@ graph LR; C --> D(Retrieve Matching Documents); ``` -### Note: Performance Considerations +::: note: Performance Considerations When using text search in MongoDB: - Text indexes can significantly improve search performance for string content. - Consider the language-specific settings for stemming and stop words. - Optimize text indexes based on the search requirements. - +::: ### Table: Text Search Options | Option | Description | diff --git a/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md b/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md index 696cb6ab0..3c2124b32 100644 --- a/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md +++ b/docs/MongoDB/Advanced MongoDB/working-with-rockmorgo.md @@ -28,10 +28,11 @@ graph LR; D --> E(Login with Default Credentials); ``` -### Note: Default Credentials +:::note Default Credentials Upon installation, RockMongo uses default credentials (Username: admin, Password: admin). Change these credentials for security purposes. +::: ## Basic Operations with RockMongo Let's explore some basic operations you can perform with RockMongo: