Databases 23 min read

What Is MongoDB? Features, Commands, and Best Practices Explained

This article provides a comprehensive overview of MongoDB, covering its definition, document‑oriented storage, key features, differences from relational databases, sharding and replication mechanisms, common commands, data types, indexing, aggregation, and practical usage scenarios for modern applications.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
What Is MongoDB? Features, Commands, and Best Practices Explained

1. What is MongoDB?

MongoDB is an open‑source, document‑oriented database written in C++. It stores data as BSON documents (similar to JSON) and can scale horizontally by adding nodes, providing high‑performance storage for web applications.

2. Key Features of MongoDB

Document‑oriented storage with a simple API.

Index any field for fast sorting.

Horizontal scaling via sharding.

Automatic data replication for high availability.

Rich query language using JSON‑style expressions.

Update operations replace whole documents or specific fields.

Map/Reduce for batch processing and aggregation.

GridFS for storing large files.

Server‑side JavaScript execution.

3. What is NoSQL and How Does It Differ from RDBMS?

NoSQL (Not Only SQL) databases store data as key‑value pairs, documents, columns, or graphs instead of fixed tables. They excel at handling unstructured or semi‑structured big data and scale horizontally, whereas relational databases offer mature ACID guarantees and complex joins.

4. Types of NoSQL Databases

Examples include MongoDB, Cassandra, CouchDB, Hypertable, Redis, Riak, HBase, Memcached, etc.

5. Basic Differences Between MySQL and MongoDB

They differ in data representation, query language, schema design, normalization, transaction support, and performance characteristics.

6. Comparing MongoDB, CouchDB, and CouchBase

All are document‑oriented NoSQL databases, but they differ in data model implementation, APIs, storage engines, and replication methods.

7. Why MongoDB Is Considered a Top NoSQL Database

File‑oriented design

High performance

High availability

Easy scalability

Rich query language

8. Journal Replay on Incomplete Entries

Journal writes are atomic; incomplete groups are not replayed during recovery.

9. Role of the Analyzer (Profiler)

The profiler records performance characteristics of operations, helping identify slow queries and suggesting index creation.

10. What Is a Namespace?

A namespace is the combination of database name and collection name (e.g., mydb.mycollection).

11. Deleting a Property from a Document

Removing a field and re‑saving the document deletes the property from storage.

12. Using Journaling for Secure Backups

Journaling can be leveraged for reliable backups.

13. Allowing Null Values

Fields can be null, but an empty object ( {}) is stored instead of a pure null value.

14. Immediate fsync on Updates

Writes are buffered by default and flushed to disk after a short delay (typically within seconds).

15. Transactions and Locking

MongoDB uses a lightweight, lock‑free design; it does not provide traditional multi‑document transactions.

16. Why Data Files Appear Large

MongoDB pre‑allocates space to reduce file fragmentation.

17. Backup Failover Time

Failover to a secondary typically takes 10–30 seconds; writes fail during this window, but reads can continue in eventual consistency mode.

18. Primary (Master) Node

The primary handles all write operations in a replica set; a new primary is elected after failover.

19. Secondary (Slave) Node

Secondaries replicate the oplog from the primary.

20. Need for getLastError

Calling getLastError confirms write acknowledgment but is not required for the write to succeed.

21. Sharded vs. Unsharded Deployment

Start with an unsharded deployment unless a single server cannot hold the initial dataset; sharding can be added later.

22. How Sharding and Replication Work

Shards are logical partitions that may consist of a single server or a replica set; replication copies data across nodes for redundancy.

23. When Data Is Distributed Across Multiple Shards

MongoDB shards by range; when a collection exceeds the default chunk size (64 MB), additional shards can store the extra chunks.

24. Updating a Document During Chunk Migration

Updates are applied to the original shard and then replicated to the destination shard before ownership transfer completes.

25. Querying a Stopped or Slow Shard

If a shard stops, queries return an error unless the "partial" option is set; slow shards cause the query to wait for a response.

26. Deleting Old moveChunk Files

Temporary files created during balancing can be manually removed after the operation completes.

27. Viewing Active Connections

db._adminCommand("connPoolStats")

28. Handling Failed moveChunk Operations

Failed moves are deterministic; the operation retries until successful, and data ends up only on the target shard.

29. Mixing Journaling with Replication

It is possible to enable journaling on some members of a replica set while disabling it on others.

30. Index Usage on Compound Keys

An index on {A:1, B:1} is used only for queries that match the exact order {A:..., B:...}, not {B:..., A:...}.

31. Stored Procedures Support

MongoDB stores JavaScript functions in the db.system.js collection as server‑side procedures.

32. GridFS Mechanism

GridFS splits large files into smaller chunks stored as documents, overcoming BSON size limits.

33. MongoDB Data Types

Supported types include String, Integer, Double, Boolean, Object, ObjectId, Array, Min/Max Key, Date, Code, Regular Expression, etc.

34. Purpose of the "Code" Data Type

It stores JavaScript code within a document.

35. Purpose of the "Regular Expression" Data Type

It stores regex patterns for query matching.

36. Purpose of the "ObjectId" Data Type

ObjectId uniquely identifies a document; it consists of a timestamp, machine identifier, process identifier, and an incrementing counter.

37. What Is an Index?

Indexes improve query performance by allowing the server to locate documents without scanning the entire collection.

38. Adding an Index

db.collectionName.createIndex({columnName:1})

39. Formatting Query Results

db.collectionName.find().pretty()

40. AND / OR Queries

Multiple key/value pairs in find() are treated as AND; use $or for OR conditions.

41. Updating Documents

Use update() to modify fields or save() to replace an entire document.

42. Deleting Documents

db.collectionName.remove({key:value})

43. Sorting Results

db.collectionName.find({key:value}).sort({columnName:1})

44. Aggregation

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

45. Replica Sets

A replica set consists of one primary and multiple secondary nodes; secondaries replicate the primary to provide redundancy and high availability.

46. Common Shell Commands

show dbs
db.dropDatabase()
db.createCollection("collectionName")
show collections
db.CollectionName.drop()

These commands list databases, drop a database, create a collection, list collections, and drop a collection respectively.

Article originally published by Distributed Lab (copyright belongs to the original author).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseshardingReplicationMongoDBNoSQLaggregation
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.