Skip to content

Commit 4243698

Browse files
authored
Merge pull request #854 from Vipullakum007/mongo-docs-8-to-15
MongoDB-docs-8-to-15 added
2 parents e8331dc + 9421bda commit 4243698

7 files changed

+1063
-0
lines changed

docs/MongoDB/create-collection.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
id: mongodb-create-collection
3+
title: MongoDB - Create Collection
4+
sidebar_label: Create Collection
5+
sidebar_position: 8
6+
tags: [mongodb, create collection, commands]
7+
description: Learn how to create a collection in MongoDB using the createCollection() method, along with examples, options, and additional commands.
8+
---
9+
10+
# MongoDB - Create Collection
11+
12+
In this chapter, we will see how to create a collection using MongoDB.
13+
14+
## The `createCollection()` Method
15+
16+
MongoDB `db.createCollection(name, options)` is used to create a collection.
17+
18+
### Syntax
19+
20+
The basic syntax of the `createCollection()` command is as follows:
21+
22+
```sql
23+
db.createCollection(name, options)
24+
```
25+
26+
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.
27+
28+
### Parameters
29+
30+
| Parameter | Type | Description |
31+
|-----------|--------|------------------------------------------------------------------|
32+
| name | String | Name of the collection to be created |
33+
| options | Document | (Optional) Specify options about memory size and indexing |
34+
35+
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:
36+
37+
### Options
38+
39+
| Field | Type | Description |
40+
|---------------|---------|--------------------------------------------------------------------------------------------------------------------|
41+
| 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. |
42+
| autoIndexId | Boolean | (Optional) If true, automatically creates an index on the `_id` field. Default value is false. |
43+
| 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. |
44+
| max | Number | (Optional) Specifies the maximum number of documents allowed in the capped collection. |
45+
46+
While inserting the document, MongoDB first checks the `size` field of the capped collection, then it checks the `max` field.
47+
48+
### Examples
49+
50+
The basic syntax of the `createCollection()` method without options is as follows:
51+
52+
```bash
53+
> use test
54+
switched to db test
55+
> db.createCollection("mycollection")
56+
{ "ok" : 1 }
57+
```
58+
59+
You can check the created collection by using the command `show collections`.
60+
61+
```bash
62+
> show collections
63+
mycollection
64+
system.indexes
65+
```
66+
67+
The following example shows the syntax of the `createCollection()` method with a few important options:
68+
69+
```bash
70+
> db.createCollection("mycol", { capped: true, autoIndexId: true, size: 6142800, max: 10000 })
71+
{
72+
"ok" : 0,
73+
"errmsg" : "BSON field 'create.autoIndexId' is an unknown field.",
74+
"code" : 40415,
75+
"codeName" : "Location40415"
76+
}
77+
```
78+
79+
In MongoDB, you don't need to create a collection explicitly. MongoDB creates the collection automatically when you insert some document.
80+
81+
```bash
82+
> db.tutorialspoint.insert({"name" : "tutorialspoint"})
83+
WriteResult({ "nInserted" : 1 })
84+
> show collections
85+
mycol
86+
mycollection
87+
system.indexes
88+
tutorialspoint
89+
```
90+
91+
## Collection Creation Diagram
92+
93+
```mermaid
94+
graph TD
95+
A[Create Collection] --> B[createCollection Method]
96+
B --> C[Syntax]
97+
B --> D[Parameters]
98+
D --> E[name]
99+
D --> F[options]
100+
C --> G[Example without options]
101+
C --> H[Example with options]
102+
H --> I[Insert Document]
103+
I --> J[Check Collections]
104+
```
105+
106+
## Command Summary
107+
108+
| Command | Description |
109+
|--------------------------------------------|-----------------------------------------------------------------------------|
110+
| `db.createCollection(name, options)` | Create a collection with the specified name and options |
111+
| `show collections` | List all collections in the current database |
112+
| `db.collection.insert(document)` | Insert a document into a collection, creating the collection if it does not exist |

docs/MongoDB/datatypes.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
id: mongodb-datatypes
3+
title: MongoDB - Datatypes
4+
sidebar_label: Datatypes
5+
sidebar_position: 10
6+
tags: [mongodb, datatypes, commands]
7+
description: Learn about the various datatypes supported by MongoDB, their uses, and examples.
8+
---
9+
10+
# MongoDB - Datatypes
11+
12+
MongoDB supports many datatypes. Some of them are listed below:
13+
14+
15+
| Datatype | Description |
16+
|--------------------|-----------------------------------------------------------------------------|
17+
| **String** | Stores UTF-8 valid strings. |
18+
| **Integer** | Stores 32-bit or 64-bit numerical values. |
19+
| **Boolean** | Stores a boolean value (true/false). |
20+
| **Double** | Stores floating-point values. |
21+
| **Min/Max Keys** | Used to compare values against the lowest and highest BSON elements. |
22+
| **Arrays** | Stores arrays or lists of multiple values. |
23+
| **Timestamp** | Stores timestamps, useful for recording modification or addition times. |
24+
| **Object** | Used for embedded documents. |
25+
| **Null** | Stores a Null value. |
26+
| **Symbol** | Used identically to a string, reserved for specific languages. |
27+
| **Date** | Stores the current date/time in UNIX format. |
28+
| **Object ID** | Stores the document’s ID. |
29+
| **Binary Data** | Stores binary data. |
30+
| **Code** | Stores JavaScript code in the document. |
31+
| **Regular Expression** | Stores regular expressions. |
32+
33+
34+
## Datatype Diagram
35+
36+
```mermaid
37+
graph TD
38+
A[Datatypes] --> B[String]
39+
A --> C[Integer]
40+
A --> D[Boolean]
41+
A --> E[Double]
42+
A --> F[Min/Max Keys]
43+
A --> G[Arrays]
44+
A --> H[Timestamp]
45+
A --> I[Object]
46+
A --> J[Null]
47+
A --> K[Symbol]
48+
A --> L[Date]
49+
A --> M[Object ID]
50+
A --> N[Binary Data]
51+
A --> O[Code]
52+
A --> P[Regular Expression]
53+
```

docs/MongoDB/delete-document.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
id: mongodb-delete-document
3+
title: delete Document
4+
sidebar_label: Delete Document
5+
sidebar_position: 14
6+
tags: [mongodb, delete, document, commands]
7+
description: Learn how to Delete documents in MongoDB collections using different methods.
8+
---
9+
10+
# MongoDB - Delete Document
11+
12+
In this chapter, we will learn how to delete a document using MongoDB.
13+
14+
## The remove() Method
15+
16+
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.
17+
18+
- **deletion criteria** − (Optional) The criteria according to which documents will be removed.
19+
- **justOne** − (Optional) If set to `true` or `1`, then only one document will be removed.
20+
21+
### Syntax
22+
23+
The basic syntax of the `remove()` method is as follows:
24+
25+
```bash
26+
> db.COLLECTION_NAME.remove(DELETION_CRITERIA)
27+
```
28+
29+
### Example
30+
31+
Consider the `mycol` collection has the following data:
32+
33+
```json
34+
{ "_id" : ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview" },
35+
{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" },
36+
{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" }
37+
```
38+
39+
Following example will remove all the documents whose title is 'MongoDB Overview'.
40+
41+
```bash
42+
> db.mycol.remove({'title':'MongoDB Overview'})
43+
WriteResult({ "nRemoved" : 1 })
44+
> db.mycol.find()
45+
{ "_id" : ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview" }
46+
{ "_id" : ObjectId("507f191e810c19729de860e3"), "title": "Tutorials Point Overview" }
47+
```
48+
49+
### Remove Only One
50+
51+
If there are multiple records and you want to delete only the first record, then set the `justOne` parameter in the `remove()` method.
52+
53+
```bash
54+
> db.COLLECTION_NAME.remove(DELETION_CRITERIA, 1)
55+
```
56+
57+
### Remove All Documents
58+
59+
If you don't specify deletion criteria, then MongoDB will delete all documents from the collection. This is equivalent to SQL's `TRUNCATE` command.
60+
61+
```bash
62+
> db.mycol.remove({})
63+
WriteResult({ "nRemoved" : 2 })
64+
> db.mycol.find()
65+
```
66+
67+
## Diagrams
68+
69+
### Remove Document Flow
70+
```mermaid
71+
graph TD;
72+
A[Start] --> B[Specify Collection]
73+
B --> C{Set Deletion Criteria?}
74+
C -->|Yes| D[Specify Criteria]
75+
C -->|No| E[Proceed Without Criteria]
76+
D --> F{Set justOne Flag?}
77+
F -->|Yes| G[Set justOne to true/1]
78+
F -->|No| H[Set justOne to false/0]
79+
G --> I[Execute remove method]
80+
H --> I[Execute remove method]
81+
E --> I[Execute remove method]
82+
I --> J[Documents Deleted]
83+
J --> K[End]
84+
```
85+
86+
## Notes
87+
88+
- Always double-check your deletion criteria to avoid accidental removal of unintended documents.
89+
- Use `justOne` flag to limit deletion to a single document if needed.
90+
- Omitting the deletion criteria will result in removing all documents from the collection.
91+
92+
## Table of Commands
93+
94+
| Command | Description |
95+
| ------- | ----------- |
96+
| `db.COLLECTION_NAME.remove(DELETION_CRITERIA)` | Removes documents matching the criteria. |
97+
| `db.COLLECTION_NAME.remove(DELETION_CRITERIA, 1)` | Removes only the first document matching the criteria. |
98+
| `db.COLLECTION_NAME.remove({})` | Removes all documents from the collection. |

docs/MongoDB/drop-collection.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
id: mongodb-drop-collection
3+
title: MongoDB - Drop Collection
4+
sidebar_label: Drop Collection
5+
sidebar_position: 9
6+
tags: [mongodb, drop collection, commands]
7+
description: Learn how to drop a collection in MongoDB using the drop() method, along with examples, additional commands, and a detailed diagram.
8+
---
9+
10+
# MongoDB - Drop Collection
11+
12+
In this chapter, we will see how to drop a collection using MongoDB.
13+
14+
## The `drop()` Method
15+
16+
MongoDB's `db.collection.drop()` is used to drop a collection from the database.
17+
18+
### Syntax
19+
20+
The basic syntax of the `drop()` command is as follows:
21+
22+
```sql
23+
db.COLLECTION_NAME.drop()
24+
```
25+
26+
### Example
27+
28+
First, check the available collections in your database `mydb`.
29+
30+
```bash
31+
> use mydb
32+
switched to db mydb
33+
> show collections
34+
mycol
35+
mycollection
36+
system.indexes
37+
tutorialspoint
38+
```
39+
40+
Now drop the collection with the name `mycollection`.
41+
42+
```bash
43+
> db.mycollection.drop()
44+
true
45+
```
46+
47+
Again, check the list of collections in the database.
48+
49+
```bash
50+
> show collections
51+
mycol
52+
system.indexes
53+
tutorialspoint
54+
```
55+
56+
The `drop()` method will return `true` if the selected collection is dropped successfully; otherwise, it will return `false`.
57+
58+
## Drop Collection Diagram
59+
60+
```mermaid
61+
graph TD
62+
A[Drop Collection] --> B[drop Method]
63+
B --> C[Syntax]
64+
B --> D[Example]
65+
D --> E[Check Collections]
66+
E --> F[Drop Collection]
67+
F --> G[Verify Collection Dropped]
68+
```
69+
70+
## Command Summary
71+
72+
| Command | Description |
73+
|-------------------------------|------------------------------------------------------|
74+
| `db.COLLECTION_NAME.drop()` | Drop the specified collection from the database |
75+
| `show collections` | List all collections in the current database |
76+
| `use DATABASE_NAME` | Switch to the specified database |

0 commit comments

Comments
 (0)