Skip to content

Commit f869fb5

Browse files
authored
Update README.md
1 parent dfe6723 commit f869fb5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,74 @@
11
# mongodb_cheatsheet
22
Handlful cheatsheet which you can carry while using mongodb no need to search each commands on browser
3+
4+
MongoDB Shell Cheatsheet
5+
6+
Basic Commands:
7+
8+
1. Start MongoDB Shell:
9+
mongo
10+
11+
2. View Databases:
12+
show dbs
13+
14+
3. Switch Database:
15+
use mydatabase
16+
17+
4. View Collections in Current Database:
18+
show collections
19+
20+
5. Create Database:
21+
use mydatabase
22+
23+
6. Create Collection:
24+
db.createCollection("mycollection")
25+
26+
7. Insert Document into Collection:
27+
db.mycollection.insertOne({ key: "value" })
28+
29+
8. Find Documents in Collection:
30+
db.mycollection.find()
31+
32+
9. Update Document in Collection:
33+
db.mycollection.updateOne({ key: "value" }, { $set: { key: "new value" } })
34+
35+
10. Remove Document from Collection:
36+
db.mycollection.deleteOne({ key: "value" })
37+
38+
11. Drop Collection:
39+
db.mycollection.drop()
40+
41+
12. Drop Database:
42+
use mydatabase
43+
db.dropDatabase()
44+
45+
46+
Advanced Commands:
47+
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:
54+
db.mycollection.aggregate([...])
55+
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")

0 commit comments

Comments
 (0)