Skip to content

MongoDB docs 15 to 20 added #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions docs/MongoDB/mongodb-aggregation.md
Original file line number Diff line number Diff line change
@@ -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
180 changes: 180 additions & 0 deletions docs/MongoDB/mongodb-indexing.md
Original file line number Diff line number Diff line change
@@ -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.
99 changes: 99 additions & 0 deletions docs/MongoDB/mongodb-limit-records.md
Original file line number Diff line number Diff line change
@@ -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. |
Loading
Loading