diff --git a/docs/MongoDB/mongodb-aggregation.md b/docs/MongoDB/mongodb-aggregation.md new file mode 100644 index 000000000..69906462e --- /dev/null +++ b/docs/MongoDB/mongodb-aggregation.md @@ -0,0 +1,89 @@ +--- +id: mongodb-aggregation +title: MongoDB - Aggregation +sidebar_label: Aggregation +sidebar_position: 19 +tags: [mongodb, aggregation, database, operations] +description: Learn how to perform aggregation operations in MongoDB to process and compute data results. +--- + +# MongoDB - Aggregation + +Aggregation operations in MongoDB process data records and return computed results. These operations group values from multiple documents together and can perform a variety of operations on the grouped data to return a single result. + +## The aggregate() Method + +For aggregation in MongoDB, you should use the `aggregate()` method. + +### Syntax + +The basic syntax of the `aggregate()` method is as follows: + +```shell +>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) +``` + +### Example + +In the collection, you have the following data: + +```json +[ + { + "_id": ObjectId("7df78ad8902c"), + "title": "MongoDB Overview", + "description": "MongoDB is a NoSQL database", + "by_user": "tutorials point", + "url": "http://www.tutorialspoint.com", + "tags": ["mongodb", "database", "NoSQL"], + "likes": 100 + }, + { + "_id": ObjectId("7df78ad8902d"), + "title": "NoSQL Overview", + "description": "NoSQL database is very fast", + "by_user": "tutorials point", + "url": "http://www.tutorialspoint.com", + "tags": ["mongodb", "database", "NoSQL"], + "likes": 10 + }, + { + "_id": ObjectId("7df78ad8902e"), + "title": "Neo4j Overview", + "description": "Neo4j is a NoSQL database", + "by_user": "Neo4j", + "url": "http://www.neo4j.com", + "tags": ["neo4j", "database", "NoSQL"], + "likes": 750 + } +] +``` + +Now, from the above collection, if you want to display a list stating how many tutorials are written by each user, then you will use the following `aggregate()` method: + +```shell +> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) +{ "_id" : "tutorials point", "num_tutorial" : 2 } +{ "_id" : "Neo4j", "num_tutorial" : 1 } +``` + +In the above example, we have grouped documents by the field `by_user` and counted the number of tutorials written by each user. + +## Aggregation Expressions + +Here are some common aggregation expressions used with the `aggregate()` method: + +| Expression | Description | Example | +|------------|-------------|---------| +| $sum | Sums up the defined value from all documents in the collection. | `db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])` | +| $avg | Calculates the average of all given values from all documents in the collection. | `db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])` | +| $min | Gets the minimum of the corresponding values from all documents in the collection. | `db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])` | +| $max | Gets the maximum of the corresponding values from all documents in the collection. | `db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])` | +| $push | Inserts the value into an array in the resulting document. | `db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])` | +| $addToSet | Inserts the value into an array in the resulting document but does not create duplicates. | `db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])` | +| $first | Gets the first document from the source documents according to the grouping. | `db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])` | +| $last | Gets the last document from the source documents according to the grouping. | `db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])` | + +## Pipeline Concept + +MongoDB supports the pipeline concept in the aggregation framework, similar to UNIX command shell pipelines. The pipeline allows executing operations on input data \ No newline at end of file diff --git a/docs/MongoDB/mongodb-indexing.md b/docs/MongoDB/mongodb-indexing.md new file mode 100644 index 000000000..a7d923f50 --- /dev/null +++ b/docs/MongoDB/mongodb-indexing.md @@ -0,0 +1,180 @@ +--- +id: mongodb-indexing +title: MongoDB - Indexing +sidebar_label: Indexing +sidebar_position: 18 +tags: [mongodb, indexing, database, commands] +description: Learn how to create and manage indexes in MongoDB to improve query performance. +--- + +# MongoDB - Indexing + +Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and requires MongoDB to process a large volume of data. + +Indexes are special data structures that store a small portion of the data set in an easy-to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index. + +## The createIndex() Method + +To create an index, you need to use the `createIndex()` method of MongoDB. + +### Syntax + +The basic syntax of the `createIndex()` method is as follows: + +```shell +>db.COLLECTION_NAME.createIndex({KEY:1}) +``` + +Here, `KEY` is the name of the field on which you want to create an index, and `1` is for ascending order. To create an index in descending order, you need to use `-1`. + +### Example + +```shell +>db.mycol.createIndex({"title":1}) +{ + "createdCollectionAutomatically" : false, + "numIndexesBefore" : 1, + "numIndexesAfter" : 2, + "ok" : 1 +} +``` + +In the `createIndex()` method, you can pass multiple fields to create an index on multiple fields. + +```shell +>db.mycol.createIndex({"title":1,"description":-1}) +``` + +## Index Options + +This method also accepts a list of options (which are optional). Following is the list: + +| Parameter | Type | Description | +|---------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------| +| background | Boolean | Builds the index in the background so that building an index does not block other database activities. Default value is `false`. | +| unique | Boolean | Creates a unique index so that the collection will not accept insertion of documents where the index key matches an existing value. Specify `true` to create a unique index. Default value is `false`. | +| name | String | The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order. | +| sparse | Boolean | If `true`, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). Default value is `false`. | +| expireAfterSeconds | Integer | Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection. | +| weights | Document | The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to other indexed fields in terms of the score. | +| default_language | String | For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. Default value is `English`. | +| language_override | String | For a text index, specify the name of the field in the document that contains the language to override the default language. Default value is `language`. | + +## The dropIndex() Method + +You can drop a particular index using the `dropIndex()` method of MongoDB. + +### Syntax + +The basic syntax of the `dropIndex()` method is as follows: + +```shell +>db.COLLECTION_NAME.dropIndex({KEY:1}) +``` + +Here, `KEY` is the name of the field on which you want to remove an existing index. Instead of the index specification document (above syntax), you can also specify the name of the index directly as: + +```shell +dropIndex("name_of_the_index") +``` + +### Example + +```shell +> db.mycol.dropIndex({"title":1}) +{ + "ok" : 0, + "errmsg" : "can't find index with key: { title: 1.0 }", + "code" : 27, + "codeName" : "IndexNotFound" +} +``` + +## The dropIndexes() Method + +This method deletes multiple (specified) indexes on a collection. + +### Syntax + +The basic syntax of the `dropIndexes()` method is as follows: + +```shell +>db.COLLECTION_NAME.dropIndexes() +``` + +### Example + +Assume we have created 2 indexes in the named `mycol` collection as shown below: + +```shell +> db.mycol.createIndex({"title":1,"description":-1}) +``` + +Following example removes the above-created indexes of `mycol`: + +```shell +>db.mycol.dropIndexes({"title":1,"description":-1}) +{ "nIndexesWas" : 2, "ok" : 1 } +``` + +## The getIndexes() Method + +This method returns the description of all the indexes in the collection. + +### Syntax + +Following is the basic syntax of the `getIndexes()` method: + +```shell +db.COLLECTION_NAME.getIndexes() +``` + +### Example + +Assume we have created 2 indexes in the named `mycol` collection as shown below: + +```shell +> db.mycol.createIndex({"title":1,"description":-1}) +``` + +Following example retrieves all the indexes in the collection `mycol`: + +```shell +> db.mycol.getIndexes() +[ + { + "v" : 2, + "key" : { + "_id" : 1 + }, + "name" : "_id_", + "ns" : "test.mycol" + }, + { + "v" : 2, + "key" : { + "title" : 1, + "description" : -1 + }, + "name" : "title_1_description_-1", + "ns" : "test.mycol" + } +] +``` + +## Diagram + +```mermaid +graph TD; + A["Start"] --> B["Specify Collection"] + B --> C{"Create Index"} + C -->|Single Field| D["createIndex({KEY:1})"] + C -->|Multiple Fields| E["createIndex({KEY1:1, KEY2:-1})"] + D --> F["Index Created"] + E --> F["Index Created"] + F --> G["Use Index in Queries"] + G --> H["End"] +``` + +## Note +> Indexes can greatly improve query performance, but they also come with overhead. It's important to balance the benefits of indexing with the cost of maintaining them, especially for write-heavy operations. diff --git a/docs/MongoDB/mongodb-limit-records.md b/docs/MongoDB/mongodb-limit-records.md new file mode 100644 index 000000000..cfcf06468 --- /dev/null +++ b/docs/MongoDB/mongodb-limit-records.md @@ -0,0 +1,99 @@ +--- +id: mongodb-limit-records +title: MongoDB - Limit Records +sidebar_label: Limit Records +sidebar_position: 16 +tags: [mongodb, limit, records, commands] +description: Learn how to limit the number of records returned in a MongoDB query using the limit() method. +--- + +# MongoDB - Limit Records + +In this chapter, we will learn how to limit records using MongoDB. + +## The limit() Method + +To limit the records in MongoDB, you need to use the `limit()` method. The method accepts one number type argument, which is the number of documents that you want to be displayed. + +### Syntax + +The basic syntax of the `limit()` method is as follows: + +```bash +> db.COLLECTION_NAME.find().limit(NUMBER) +``` + +### Example + +Consider the collection `mycol` has the following data: + +```json +{ "_id" : ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" } +``` + +The following example will display only two documents while querying the document: + +```bash +> db.mycol.find({},{"title":1,_id:0}).limit(2) +{"title":"MongoDB Overview"} +{"title":"NoSQL Overview"} +``` + +If you don't specify the number argument in the `limit()` method, it will display all documents from the collection. + +## MongoDB skip() Method + +Apart from the `limit()` method, there is one more method `skip()` which also accepts a number type argument and is used to skip the number of documents. + +### Syntax + +The basic syntax of the `skip()` method is as follows: + +```bash +> db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) +``` + +### Example + +The following example will display only the second document: + +```bash +> db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1) +{"title":"NoSQL Overview"} +``` + +Please note, the default value in the `skip()` method is 0. + +## Diagram + +### Limit and Skip Operation Flow +```mermaid +graph TD; + A[Start] --> B[Specify Collection] + B --> C{Set Limit} + C -->|Yes| D[Apply limit Method] + D --> E{Set Skip} + E -->|Yes| F[Apply skip Method] + E -->|No| G[Execute find with Limit] + F --> G[Execute find with Limit and Skip] + G --> H[Display Results] + H --> I[End] +``` + +## Notes + +> - The `limit()` method is used to restrict the number of documents returned in the query result. +> - The `skip()` method is used to skip a specified number of documents in the query result. +> - Combining `limit()` and `skip()` allows for pagination of results. +> - The default value for `skip()` is 0, meaning no documents are skipped. + +## Table of Commands + +| Command | Description | +| ------- | ----------- | +| `db.COLLECTION_NAME.find().limit(NUMBER)` | Limits the number of documents returned. | +| `db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)` | Limits and skips the number of documents returned. | +| `db.COLLECTION_NAME.find({}, {"title": 1, "_id": 0}).limit(2)` | Retrieves two documents with only the `title` field. | +| `db.COLLECTION_NAME.find({}, {"title": 1, "_id": 0}).limit(1).skip(1)` | Retrieves the second document with only the `title` field. | diff --git a/docs/MongoDB/mongodb-projection.md b/docs/MongoDB/mongodb-projection.md new file mode 100644 index 000000000..3a8b9b6b5 --- /dev/null +++ b/docs/MongoDB/mongodb-projection.md @@ -0,0 +1,73 @@ +--- +id: mongodb-projection +title: MongoDB - Projection +sidebar_label: Projection +sidebar_position: 15 +tags: [mongodb, projection, document, commands] +description: Learn how to select only necessary fields from documents in MongoDB collections using projection. +--- + +# MongoDB - Projection + +In MongoDB, projection means selecting only the necessary data rather than selecting the whole data of a document. If a document has 5 fields and you need to show only 3, then select only those 3 fields. + +## The find() Method + +MongoDB's `find()` method, explained in MongoDB Query Document, accepts a second optional parameter that is a list of fields that you want to retrieve. In MongoDB, when you execute the `find()` method, it displays all fields of a document. To limit this, you need to set a list of fields with value 1 or 0. `1` is used to show the field while `0` is used to hide the fields. + +### Syntax + +The basic syntax of the `find()` method with projection is as follows: + +```bash +> db.COLLECTION_NAME.find({},{KEY:1}) +``` + +### Example + +Consider the collection `mycol` has the following data: + +```json +{ "_id" : ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" } +``` + +The following example will display the title of the document while querying the document: + +```bash +> db.mycol.find({},{"title":1,_id:0}) +{"title":"MongoDB Overview"} +{"title":"NoSQL Overview"} +{"title":"Tutorials Point Overview"} +``` + +Please note that the `_id` field is always displayed while executing the `find()` method. If you don't want this field, then you need to set it as `0`. + +## Diagrams + +### Projection Operation Flow +```mermaid +graph TD; + A[Start] --> B[Specify Collection] + B --> C{Set Projection Criteria} + C -->|Include Fields| D[Set Field to 1] + C -->|Exclude Fields| E[Set Field to 0] + D --> F[Execute find with Projection] + E --> F[Execute find with Projection] + F --> G[Display Results] + G --> H[End] +``` + +> **Notes:** +> - Projections are used to retrieve only the necessary data, reducing the amount of data transferred and improving query performance. +> - The `_id` field is included by default in projections. To exclude it, explicitly set `_id: 0`. +> - Use `1` to include a field in the projection and `0` to exclude it. + +## Table of Commands + +| Command | Description | +| ------- | ----------- | +| `db.COLLECTION_NAME.find({}, {KEY: 1})` | Includes specified fields in the results. | +| `db.COLLECTION_NAME.find({}, {KEY: 0})` | Excludes specified fields from the results. | +| `db.COLLECTION_NAME.find({}, {"title": 1, "_id": 0})` | Retrieves only the `title` field, excluding the `_id` field. | diff --git a/docs/MongoDB/mongodb-replication.md b/docs/MongoDB/mongodb-replication.md new file mode 100644 index 000000000..54a5fa832 --- /dev/null +++ b/docs/MongoDB/mongodb-replication.md @@ -0,0 +1,86 @@ +--- +id: mongodb-replication +title: MongoDB - Replication +sidebar_label: Replication +sidebar_position: 20 +tags: [mongodb, replication, database, replica set] +description: Learn about MongoDB replication, its benefits, how it works, and how to set up and manage replica sets. +--- + +# MongoDB - Replication + +Replication in MongoDB is the process of synchronizing data across multiple servers. It provides redundancy, increases data availability, and allows for disaster recovery and high availability of data. + +## Why Replication? + +- **Data Safety**: Keeps your data safe with multiple copies. +- **High Availability**: Ensures 24/7 availability of data. +- **Disaster Recovery**: Helps in recovering from disasters. +- **No Downtime**: No downtime for maintenance activities. +- **Read Scaling**: Provides extra copies for reading operations. +- **Transparent to Application**: Replica set is transparent to applications. + +## How Replication Works in MongoDB + +MongoDB achieves replication through the use of a replica set, which is a group of mongod instances hosting the same data set. Here's how it works: + +```mermaid +graph TD; + A[Client Application] --> B[Primary Node] + B -->|Replicates data| C[Secondary Node 1] + B -->|Replicates data| D[Secondary Node 2] + B -->|Replicates data| E[Secondary Node N] + C --> F[Automatic Failover] + D --> F + E --> F + F --> G[Recovery] +``` + +### Replica Set Features + +- Cluster of N nodes. +- Any one node can be primary. +- All write operations go to the primary. +- Automatic failover and recovery. +- Consensus election of primary. + +## Set Up a Replica Set + +To set up a replica set, follow these steps: + +1. Shutdown the running MongoDB server. +2. Start the MongoDB server with the `--replSet` option: + + ```shell + mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME" + ``` + + Example: + ```shell + mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0 + ``` + +3. Connect to this mongod instance and issue the command `rs.initiate()` to initiate a new replica set. +4. Use `rs.conf()` to check the replica set configuration and `rs.status()` to check the status of the replica set. + +## Add Members to Replica Set + +To add members to a replica set: + +1. Start mongod instances on multiple machines. +2. Connect to the primary node in the Mongo client. +3. Issue the `rs.add()` command to add members to the replica set. + +### Syntax + +```shell +>rs.add(HOST_NAME:PORT) +``` + +Example: + +```shell +>rs.add("mongod1.net:27017") +``` + +> **NOTE :** You can add mongod instances to the replica set only when connected to the primary node. Use `db.isMaster()` to check your connection status. diff --git a/docs/MongoDB/mongodb-sort-records.md b/docs/MongoDB/mongodb-sort-records.md new file mode 100644 index 000000000..033451804 --- /dev/null +++ b/docs/MongoDB/mongodb-sort-records.md @@ -0,0 +1,75 @@ +--- +id: mongodb-sort-records +title: MongoDB - Sort Records +sidebar_label: Sort Records +sidebar_position: 17 +tags: [mongodb, sort, records, commands] +description: Learn how to sort records in MongoDB collections using the sort() method. +--- + +# MongoDB - Sort Records + +In this chapter, we will learn how to sort records in MongoDB. + +## The sort() Method + +To sort documents in MongoDB, you need to use the `sort()` method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order, `1` and `-1` are used. `1` is used for ascending order while `-1` is used for descending order. + +### Syntax + +The basic syntax of the `sort()` method is as follows: + +```bash +> db.COLLECTION_NAME.find().sort({KEY:1}) +``` + +### Example + +Consider the collection `mycol` has the following data: + +```json +{ "_id" : ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" } +{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" } +``` + +The following example will display the documents sorted by title in descending order: + +```bash +> db.mycol.find({},{"title":1,_id:0}).sort({"title":-1}) +{"title":"Tutorials Point Overview"} +{"title":"NoSQL Overview"} +{"title":"MongoDB Overview"} +``` + +Please note, if you don't specify the sorting preference, the `sort()` method will display the documents in ascending order. + +## Diagram + +### Sorting Operation Flow +```mermaid +graph TD; + A[Start] --> B[Specify Collection] + B --> C{Set Sort Order} + C -->|Ascending| D["Apply sort ({KEY:1})"] + C -->|Descending| E["Apply sort ({KEY:-1})"] + D --> F["Execute find() with Sort"] + E --> F["Execute find() with Sort"] + F --> G[Display Sorted Results] + G --> H[End] +``` + +## Notes + +- The `sort()` method is used to order the documents returned in the query result. +- Sorting can be specified in ascending order (`1`) or descending order (`-1`). +- If no sorting preference is specified, documents are sorted in ascending order by default. + +## Table of Commands + +| Command | Description | +| ------- | ----------- | +| `db.COLLECTION_NAME.find().sort({KEY:1})` | Sorts the documents in ascending order by the specified key. | +| `db.COLLECTION_NAME.find().sort({KEY:-1})` | Sorts the documents in descending order by the specified key. | +| `db.COLLECTION_NAME.find({}, {"title": 1, "_id": 0}).sort({"title": -1})` | Retrieves documents sorted by the `title` field in descending order, displaying only the `title` field. | +| `db.COLLECTION_NAME.find({}, {"title": 1, "_id": 0}).sort({"title": 1})` | Retrieves documents sorted by the `title` field in ascending order, displaying only the `title` field. |