What is NoSQL?
- NoSQL (“Not Only SQL”) databases store and retrieve data differently than relational databases.
- Designed for scalability, flexibility, and high performance.
- Often used for big data, real-time applications, and distributed systems.
Popular NoSQL Databases
- MongoDB – Document store, supports ACID transactions and aggregation framework.
- Redis – In-memory key-value store, supports caching, pub/sub, and Lua scripting.
MongoDB Commands
Database Management
Commands | Description |
---|---|
show dbs | List databases |
use myDatabase | Select database |
db.createCollection("users") | Create collection |
db.users.insertOne({ name: "Alice", age: 25 }) | Insert document |
db.users.insertMany([{ name: "Bob" }, { name: "Charlie" }]) | Insert multiple documents |
Collection Management
Commands | Description |
---|---|
db.createCollection(‘users’) | Create a new collection |
db.getCollectionNames() | List all collections |
db.users.renameCollection(‘customers’) | Rename a collection |
db.users.drop() | Drop a collection |
db.users.stats() | Get collection statistics |
CRUD Operations
Commands | Description |
---|---|
db.users.insertOne({ name: ‘John’ }) | Insert a single document |
db.users.insertMany([{ name: ‘Alice’ }, { name: ‘Bob’ }]) | Insert multiple documents |
db.users.find() | Find all documents |
db.users.findOne() | Find a single document |
db.users.updateOne({ name: ‘John’ }, { $set: { age: 30 } }) | Update a single document |
Indexing
Commands | Description |
---|---|
db.users.createIndex({ name: 1 }) | Create an ascending index |
db.users.createIndex({ age: -1 }) | Create a descending index |
db.users.getIndexes() | List all indexes |
db.users.dropIndex(‘name_1’) | Drop an index |
db.users.reIndex() | Rebuild all indexes |
Security & Authentication
Commands | Description |
---|---|
db.createUser({ user: ‘admin’, pwd: ‘password’, roles: [‘readWrite’] }) | Create a new user |
db.auth(‘admin’, ‘password’) | Authenticate a user |
db.updateUser(‘admin’, { roles: [‘dbAdmin’] }) | Modify a user’s roles |
db.getUsers() | List all users |
db.dropUser(‘user’) | Remove a user |
Redis Commands
Connection & Server
Commands | Description |
---|---|
PING | Test connection (returns PONG) |
AUTH password | Authenticate to the server |
SELECT index | Change the selected database |
QUIT | Close the connection |
INFO | Get server information and statistics |
Key Management
Commands | Description |
---|---|
SET key value | Set key to hold string value |
GET key | Get value of key |
DEL key [key …] | Delete one or more keys |
EXISTS key [key …] | Check if key(s) exist |
EXPIRE key seconds | Set key expiration time in seconds |
Strings
Commands | Description |
---|---|
SET key value | Set string value of a key |
GET key | Get string value of a key |
APPEND key value | Append to string value of a key |
INCR key | Increment integer value by 1 |
DECR key | Decrement integer value by 1 |
Lists
Commands | Description |
---|---|
LPUSH key value [value …] | Prepend values to a list |
RPUSH key value [value …] | Append values to a list |
LPOP key | Remove and get first element |
RPOP key | Remove and get last element |
LLEN key | Get list length |
Sets
Commands | Description |
---|---|
SADD key member [member …] | Add members to set |
SREM key member [member …] | Remove members from set |
SMEMBERS key | Get all members in set |
SISMEMBER key member | Test if member is in set |
SCARD key | Get set cardinality (size) |