Skip to content

MongoDB-docs-8-to-15 added #854

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 9, 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
112 changes: 112 additions & 0 deletions docs/MongoDB/create-collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
id: mongodb-create-collection
title: MongoDB - Create Collection
sidebar_label: Create Collection
sidebar_position: 8
tags: [mongodb, create collection, commands]
description: Learn how to create a collection in MongoDB using the createCollection() method, along with examples, options, and additional commands.
---

# MongoDB - Create Collection

In this chapter, we will see how to create a collection using MongoDB.

## The `createCollection()` Method

MongoDB `db.createCollection(name, options)` is used to create a collection.

### Syntax

The basic syntax of the `createCollection()` command is as follows:

```sql
db.createCollection(name, options)
```

In the command, `name` is the name of the collection to be created. `Options` is a document and is used to specify the configuration of the collection.

### Parameters

| Parameter | Type | Description |
|-----------|--------|------------------------------------------------------------------|
| name | String | Name of the collection to be created |
| options | Document | (Optional) Specify options about memory size and indexing |

The `options` parameter is optional, so you need to specify only the name of the collection. Following is the list of options you can use:

### Options

| Field | Type | Description |
|---------------|---------|--------------------------------------------------------------------------------------------------------------------|
| capped | Boolean | (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify the `size` parameter also. |
| autoIndexId | Boolean | (Optional) If true, automatically creates an index on the `_id` field. Default value is false. |
| size | Number | (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also. |
| max | Number | (Optional) Specifies the maximum number of documents allowed in the capped collection. |

While inserting the document, MongoDB first checks the `size` field of the capped collection, then it checks the `max` field.

### Examples

The basic syntax of the `createCollection()` method without options is as follows:

```bash
> use test
switched to db test
> db.createCollection("mycollection")
{ "ok" : 1 }
```

You can check the created collection by using the command `show collections`.

```bash
> show collections
mycollection
system.indexes
```

The following example shows the syntax of the `createCollection()` method with a few important options:

```bash
> db.createCollection("mycol", { capped: true, autoIndexId: true, size: 6142800, max: 10000 })
{
"ok" : 0,
"errmsg" : "BSON field 'create.autoIndexId' is an unknown field.",
"code" : 40415,
"codeName" : "Location40415"
}
```

In MongoDB, you don't need to create a collection explicitly. MongoDB creates the collection automatically when you insert some document.

```bash
> db.tutorialspoint.insert({"name" : "tutorialspoint"})
WriteResult({ "nInserted" : 1 })
> show collections
mycol
mycollection
system.indexes
tutorialspoint
```

## Collection Creation Diagram

```mermaid
graph TD
A[Create Collection] --> B[createCollection Method]
B --> C[Syntax]
B --> D[Parameters]
D --> E[name]
D --> F[options]
C --> G[Example without options]
C --> H[Example with options]
H --> I[Insert Document]
I --> J[Check Collections]
```

## Command Summary

| Command | Description |
|--------------------------------------------|-----------------------------------------------------------------------------|
| `db.createCollection(name, options)` | Create a collection with the specified name and options |
| `show collections` | List all collections in the current database |
| `db.collection.insert(document)` | Insert a document into a collection, creating the collection if it does not exist |
53 changes: 53 additions & 0 deletions docs/MongoDB/datatypes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
id: mongodb-datatypes
title: MongoDB - Datatypes
sidebar_label: Datatypes
sidebar_position: 10
tags: [mongodb, datatypes, commands]
description: Learn about the various datatypes supported by MongoDB, their uses, and examples.
---

# MongoDB - Datatypes

MongoDB supports many datatypes. Some of them are listed below:


| Datatype | Description |
|--------------------|-----------------------------------------------------------------------------|
| **String** | Stores UTF-8 valid strings. |
| **Integer** | Stores 32-bit or 64-bit numerical values. |
| **Boolean** | Stores a boolean value (true/false). |
| **Double** | Stores floating-point values. |
| **Min/Max Keys** | Used to compare values against the lowest and highest BSON elements. |
| **Arrays** | Stores arrays or lists of multiple values. |
| **Timestamp** | Stores timestamps, useful for recording modification or addition times. |
| **Object** | Used for embedded documents. |
| **Null** | Stores a Null value. |
| **Symbol** | Used identically to a string, reserved for specific languages. |
| **Date** | Stores the current date/time in UNIX format. |
| **Object ID** | Stores the document’s ID. |
| **Binary Data** | Stores binary data. |
| **Code** | Stores JavaScript code in the document. |
| **Regular Expression** | Stores regular expressions. |


## Datatype Diagram

```mermaid
graph TD
A[Datatypes] --> B[String]
A --> C[Integer]
A --> D[Boolean]
A --> E[Double]
A --> F[Min/Max Keys]
A --> G[Arrays]
A --> H[Timestamp]
A --> I[Object]
A --> J[Null]
A --> K[Symbol]
A --> L[Date]
A --> M[Object ID]
A --> N[Binary Data]
A --> O[Code]
A --> P[Regular Expression]
```
98 changes: 98 additions & 0 deletions docs/MongoDB/delete-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
id: mongodb-delete-document
title: delete Document
sidebar_label: Delete Document
sidebar_position: 14
tags: [mongodb, delete, document, commands]
description: Learn how to Delete documents in MongoDB collections using different methods.
---

# MongoDB - Delete Document

In this chapter, we will learn how to delete a document using MongoDB.

## The remove() Method

MongoDB's `remove()` method is used to remove a document from the collection. The `remove()` method accepts two parameters: one is the deletion criteria and the second is the `justOne` flag.

- **deletion criteria** − (Optional) The criteria according to which documents will be removed.
- **justOne** − (Optional) If set to `true` or `1`, then only one document will be removed.

### Syntax

The basic syntax of the `remove()` method is as follows:

```bash
> db.COLLECTION_NAME.remove(DELETION_CRITERIA)
```

### Example

Consider the `mycol` collection has the following data:

```json
{ "_id" : ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview" },
{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" },
{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" }
```

Following example will remove all the documents whose title is 'MongoDB Overview'.

```bash
> db.mycol.remove({'title':'MongoDB Overview'})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find()
{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" }
{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" }
```

### Remove Only One

If there are multiple records and you want to delete only the first record, then set the `justOne` parameter in the `remove()` method.

```bash
> db.COLLECTION_NAME.remove(DELETION_CRITERIA, 1)
```

### Remove All Documents

If you don't specify deletion criteria, then MongoDB will delete all documents from the collection. This is equivalent to SQL's `TRUNCATE` command.

```bash
> db.mycol.remove({})
WriteResult({ "nRemoved" : 2 })
> db.mycol.find()
```

## Diagrams

### Remove Document Flow
```mermaid
graph TD;
A[Start] --> B[Specify Collection]
B --> C{Set Deletion Criteria?}
C -->|Yes| D[Specify Criteria]
C -->|No| E[Proceed Without Criteria]
D --> F{Set justOne Flag?}
F -->|Yes| G[Set justOne to true/1]
F -->|No| H[Set justOne to false/0]
G --> I[Execute remove method]
H --> I[Execute remove method]
E --> I[Execute remove method]
I --> J[Documents Deleted]
J --> K[End]
```

## Notes

- Always double-check your deletion criteria to avoid accidental removal of unintended documents.
- Use `justOne` flag to limit deletion to a single document if needed.
- Omitting the deletion criteria will result in removing all documents from the collection.

## Table of Commands

| Command | Description |
| ------- | ----------- |
| `db.COLLECTION_NAME.remove(DELETION_CRITERIA)` | Removes documents matching the criteria. |
| `db.COLLECTION_NAME.remove(DELETION_CRITERIA, 1)` | Removes only the first document matching the criteria. |
| `db.COLLECTION_NAME.remove({})` | Removes all documents from the collection. |
76 changes: 76 additions & 0 deletions docs/MongoDB/drop-collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
id: mongodb-drop-collection
title: MongoDB - Drop Collection
sidebar_label: Drop Collection
sidebar_position: 9
tags: [mongodb, drop collection, commands]
description: Learn how to drop a collection in MongoDB using the drop() method, along with examples, additional commands, and a detailed diagram.
---

# MongoDB - Drop Collection

In this chapter, we will see how to drop a collection using MongoDB.

## The `drop()` Method

MongoDB's `db.collection.drop()` is used to drop a collection from the database.

### Syntax

The basic syntax of the `drop()` command is as follows:

```sql
db.COLLECTION_NAME.drop()
```

### Example

First, check the available collections in your database `mydb`.

```bash
> use mydb
switched to db mydb
> show collections
mycol
mycollection
system.indexes
tutorialspoint
```

Now drop the collection with the name `mycollection`.

```bash
> db.mycollection.drop()
true
```

Again, check the list of collections in the database.

```bash
> show collections
mycol
system.indexes
tutorialspoint
```

The `drop()` method will return `true` if the selected collection is dropped successfully; otherwise, it will return `false`.

## Drop Collection Diagram

```mermaid
graph TD
A[Drop Collection] --> B[drop Method]
B --> C[Syntax]
B --> D[Example]
D --> E[Check Collections]
E --> F[Drop Collection]
F --> G[Verify Collection Dropped]
```

## Command Summary

| Command | Description |
|-------------------------------|------------------------------------------------------|
| `db.COLLECTION_NAME.drop()` | Drop the specified collection from the database |
| `show collections` | List all collections in the current database |
| `use DATABASE_NAME` | Switch to the specified database |
Loading
Loading