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.

ทำงานในเบราว์เซอร์ของคุณ ไฟล์ไม่ออกจากอุปกรณ์ ดูวิธีตรวจสอบ

…

วิธีทำ

  1. 1
    Search a task

    Such as expire or group.

  2. 2
    Pick a section

    Or search all.

  3. 3
    Compare commands

    MongoDB, Redis and SQL.

  4. 4
    Read the warnings

    For admin commands.

  5. 5
    Choose the right tool

    Documents or keys.

  6. 6
    Try in a shell

    mongosh or redis-cli.

เกี่ยวกับเครื่องมือนี้

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).

ตัวอย่างพร้อมวิธีทำ

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.

สรุปสั้น ๆ

คำถามที่พบบ่อย

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.

ฟรีทั้งหมดไม่มีค่าใช้จ่าย ไม่ต้องสมัคร
ไฟล์เป็นส่วนตัวทุกอย่างทำงานในเบราว์เซอร์ของคุณ
ใช้ง่ายในภาษาของคุณมีหลายภาษา