|
1 |
| -# mongodb_cheatsheet |
2 |
| -Handlful cheatsheet which you can carry while using mongodb no need to search each commands on browser |
| 1 | +Certainly! Below is the README content for your MongoDB cheatsheet repository: |
3 | 2 |
|
4 |
| -MongoDB Shell Cheatsheet |
| 3 | +--- |
5 | 4 |
|
6 |
| -Basic Commands: |
| 5 | +# MongoDB Cheatsheet |
7 | 6 |
|
8 |
| -1. Start MongoDB Shell: |
| 7 | +A handy cheatsheet for MongoDB users to reference basic and advanced commands without the need to search for each command online. |
| 8 | + |
| 9 | +## MongoDB Shell Cheatsheet |
| 10 | + |
| 11 | +### Basic Commands |
| 12 | + |
| 13 | +1. **Start MongoDB Shell:** |
| 14 | + ``` |
9 | 15 | mongo
|
| 16 | + ``` |
10 | 17 |
|
11 |
| -2. View Databases: |
| 18 | +2. **View Databases:** |
| 19 | + ``` |
12 | 20 | show dbs
|
| 21 | + ``` |
13 | 22 |
|
14 |
| -3. Switch Database: |
| 23 | +3. **Switch Database:** |
| 24 | + ``` |
15 | 25 | use mydatabase
|
| 26 | + ``` |
16 | 27 |
|
17 |
| -4. View Collections in Current Database: |
| 28 | +4. **View Collections in Current Database:** |
| 29 | + ``` |
18 | 30 | show collections
|
| 31 | + ``` |
19 | 32 |
|
20 |
| -5. Create Database: |
| 33 | +5. **Create Database:** |
| 34 | + ``` |
21 | 35 | use mydatabase
|
| 36 | + ``` |
22 | 37 |
|
23 |
| -6. Create Collection: |
| 38 | +6. **Create Collection:** |
| 39 | + ``` |
24 | 40 | db.createCollection("mycollection")
|
| 41 | + ``` |
25 | 42 |
|
26 |
| -7. Insert Document into Collection: |
| 43 | +7. **Insert Document into Collection:** |
| 44 | + ``` |
27 | 45 | db.mycollection.insertOne({ key: "value" })
|
| 46 | + ``` |
28 | 47 |
|
29 |
| -8. Find Documents in Collection: |
| 48 | +8. **Find Documents in Collection:** |
| 49 | + ``` |
30 | 50 | db.mycollection.find()
|
| 51 | + ``` |
31 | 52 |
|
32 |
| -9. Update Document in Collection: |
| 53 | +9. **Update Document in Collection:** |
| 54 | + ``` |
33 | 55 | db.mycollection.updateOne({ key: "value" }, { $set: { key: "new value" } })
|
| 56 | + ``` |
34 | 57 |
|
35 |
| -10. Remove Document from Collection: |
| 58 | +10. **Remove Document from Collection:** |
| 59 | + ``` |
36 | 60 | db.mycollection.deleteOne({ key: "value" })
|
| 61 | + ``` |
37 | 62 |
|
38 |
| -11. Drop Collection: |
| 63 | +11. **Drop Collection:** |
| 64 | + ``` |
39 | 65 | db.mycollection.drop()
|
| 66 | + ``` |
40 | 67 |
|
41 |
| -12. Drop Database: |
| 68 | +12. **Drop Database:** |
| 69 | + ``` |
42 | 70 | use mydatabase
|
43 | 71 | db.dropDatabase()
|
| 72 | + ``` |
44 | 73 |
|
| 74 | +### Advanced Commands |
45 | 75 |
|
46 |
| -Advanced Commands: |
| 76 | +13. **Index Management:** |
| 77 | + - Create Index: `db.mycollection.createIndex({ key: 1 })` |
| 78 | + - List Indexes: `db.mycollection.getIndexes()` |
| 79 | + - Drop Index: `db.mycollection.dropIndex("indexName")` |
47 | 80 |
|
48 |
| -13. Index Management: |
49 |
| - - Create Index: db.mycollection.createIndex({ key: 1 }) |
50 |
| - - List Indexes: db.mycollection.getIndexes() |
51 |
| - - Drop Index: db.mycollection.dropIndex("indexName") |
52 |
| - |
53 |
| -14. Aggregation Framework: |
| 81 | +14. **Aggregation Framework:** |
| 82 | + ``` |
54 | 83 | db.mycollection.aggregate([...])
|
| 84 | + ``` |
| 85 | +
|
| 86 | +15. **Data Import and Export:** |
| 87 | + - Import Data from JSON: `mongoimport --db mydatabase --collection mycollection --file data.json` |
| 88 | + - Export Data to JSON: `mongoexport --db mydatabase --collection mycollection --out data.json` |
| 89 | +
|
| 90 | +16. **User Management:** |
| 91 | + - Create User: `db.createUser({ user: "username", pwd: "password", roles: ["readWrite"] })` |
| 92 | + - List Users: `db.getUsers()` |
| 93 | + - Drop User: `db.dropUser("username")` |
| 94 | +
|
| 95 | +17. **Replica Sets and Sharding:** |
| 96 | + - Initialize Replica Set: `rs.initiate()` |
| 97 | + - Add Replica Set Member: `rs.add("hostname:port")` |
| 98 | + - Enable Sharding on Database: `sh.enableSharding("mydatabase")` |
| 99 | + - Shard Collection: `sh.shardCollection("mydatabase.mycollection", { shardKey: 1 })` |
| 100 | +
|
| 101 | +18. **Server Administration:** |
| 102 | + - Server Status: `db.serverStatus()` |
| 103 | + - Current Operations: `db.currentOp()` |
| 104 | + - Server Logs: `db.getLog("global")` |
| 105 | +
|
| 106 | +Feel free to copy and paste these commands into your MongoDB terminal for quick reference! |
| 107 | +
|
| 108 | +--- |
55 | 109 |
|
56 |
| -15. Data Import and Export: |
57 |
| - - Import Data from JSON: mongoimport --db mydatabase --collection mycollection --file data.json |
58 |
| - - Export Data to JSON: mongoexport --db mydatabase --collection mycollection --out data.json |
59 |
| - |
60 |
| -16. User Management: |
61 |
| - - Create User: db.createUser({ user: "username", pwd: "password", roles: ["readWrite"] }) |
62 |
| - - List Users: db.getUsers() |
63 |
| - - Drop User: db.dropUser("username") |
64 |
| - |
65 |
| -17. Replica Sets and Sharding: |
66 |
| - - Initialize Replica Set: rs.initiate() |
67 |
| - - Add Replica Set Member: rs.add("hostname:port") |
68 |
| - - Enable Sharding on Database: sh.enableSharding("mydatabase") |
69 |
| - - Shard Collection: sh.shardCollection("mydatabase.mycollection", { shardKey: 1 }) |
70 |
| - |
71 |
| -18. Server Administration: |
72 |
| - - Server Status: db.serverStatus() |
73 |
| - - Current Operations: db.currentOp() |
74 |
| - - Server Logs: db.getLog("global") |
| 110 | +You can copy the content and save it into your README.md file in your MongoDB cheatsheet repository. This will provide users with an easy-to-access reference for MongoDB commands. |
0 commit comments