MongoDB and Redis Command Reference
Coming from SQL? See how to do the same job in MongoDB and Redis, from finding and updating records to counters, queues, geo search and backups.
Funziona nel tuo browser. I tuoi file non lasciano mai il dispositivo. Scopri come verificarlo
…
Come si usa
- 1Search a task
Such as expire or group.
- 2Pick a section
Or search all.
- 3Compare commands
MongoDB, Redis and SQL.
- 4Read the warnings
For admin commands.
- 5Choose the right tool
Documents or keys.
- 6Try in a shell
mongosh or redis-cli.
Informazioni su questo strumento
MongoDB and Redis are the two most popular NoSQL databases, and they solve different problems. MongoDB stores JSON-like documents and queries them with a rich filter and aggregation language. Redis keeps data structures such as strings, hashes, lists, sets, sorted sets and streams in memory, with commands that make counters, caches, queues and leaderboards trivial. Developers who think in SQL often struggle to translate familiar tasks. This reference maps 45 everyday tasks across both systems, each with the SQL you would write instead, in sections for connecting, creating, reading, updating, deleting, expiry, aggregation, indexes, data structures and administration. Search in plain words such as increment, expire, group, leaderboard or nearby, and read the command for the MongoDB shell next to the Redis command. Where one system is the wrong tool, the table says so and suggests the usual pattern, and dangerous administration commands are flagged.
When to use which
- MongoDB: flexible records with nested data, rich queries and aggregation, and durable storage. - Redis: very fast access to small values, counters, sessions, caches, queues, rate limits, real-time leaderboards and pub/sub. - Many applications use both: a primary database for records and Redis in front of it for speed.
Thinking in documents
In MongoDB, data that is read together is stored together, so an order embeds its line items rather than joining another table. $lookup can join collections, but it is slower than an embedded design. Indexes matter just as in SQL; use explain to check queries.
Thinking in keys
Redis has no tables or queries over values. You design key names, such as user:42 or rate:ip:1.2.3.4, and choose a data structure for each job. Secondary lookups need extra structures you maintain yourself, such as a set of all active users.
Expiry
Redis keys can expire after a set time, ideal for sessions, one-time codes and rate limits. MongoDB removes documents with a TTL index on a date field, checked about once a minute.
Dangerous commands
KEYS with a wildcard and FLUSHALL can block or wipe a production Redis server; use SCAN instead of KEYS. db.dropDatabase in MongoDB removes everything without confirmation. Restrict these commands with access controls.
Persistence and durability
Redis keeps data in memory and saves it with snapshots or an append-only file, so configure persistence to match how much data you can afford to lose. MongoDB writes to disk with journaling and replica sets for safety.
Limits
Commands are written for mongosh and redis-cli; drivers in each programming language use similar names. The reference covers common tasks, not every operator or option, and assumes recent versions (MongoDB 7, Redis 7).
Esempio svolto
Searching increment shows db.posts.updateOne with $inc views 1 in MongoDB, INCR post:1:views in Redis and UPDATE posts SET views = views + 1 in SQL; searching leaderboard shows ZADD and ZREVRANK in Redis next to a sort in MongoDB and RANK() in SQL.
In breve
- Categoria: Sviluppo e dati
- Funziona nel browser, senza registrazione né caricamento.
- Le informazioni che inserisci restano sul tuo dispositivo.
- Disponibile in inglese.
- Contenuto rivisto l'ultima volta il:
- Fonte ufficiale: MongoDB manual (mongosh) and Redis command reference
Domande frequenti
Is Redis a database or a cache?
Both. It keeps data in memory and can persist it to disk.
How do I join in MongoDB?
With the $lookup aggregation stage, though embedding related data is often better.
Why avoid KEYS in Redis?
It scans every key and blocks the server; use SCAN.
How do expiring records work?
Redis uses EX or EXPIRE; MongoDB uses a TTL index.
Are commands the same in drivers?
Mostly; method names follow the shell commands.
Does it connect to my server?
No. It is a reference only.
