Databases 27 min read

MongoDB Essentials: 52 Common Questions Answered

This comprehensive guide covers everything from MongoDB's core concepts, data model, and architecture to practical commands for creating databases, collections, indexes, sharding, replication, aggregation, and troubleshooting, providing clear answers to over fifty frequently asked questions.

Open Source Linux
Open Source Linux
Open Source Linux
MongoDB Essentials: 52 Common Questions Answered

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, offering high‑performance storage for web applications.

2. What are MongoDB's main features?

Document‑oriented storage with a simple API.

Index any field for fast sorting.

Horizontal scaling via sharding.

Built‑in 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 a NoSQL database 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 provide flexible schemas, whereas relational databases enforce structured tables, ACID transactions, and SQL queries.

4. Types of NoSQL databases

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

5. Basic differences between MySQL and MongoDB

They differ in data representation, query language, schema design, normalization, transaction support, and performance characteristics. MySQL is relational; MongoDB is document‑oriented.

6. Comparison of MongoDB, CouchDB and CouchBase

All are open‑source document databases, but they differ in data model implementation, APIs, storage engines, and replication mechanisms.

7. Why is MongoDB considered a top NoSQL database?

File‑based storage

High performance

High availability

Easy scalability

Rich query language

8. Journal replay on incomplete entries

Write operations are atomic per journal group; incomplete groups are not replayed during recovery.

9. Role of the profiler (analyzer) in MongoDB

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

10. What is a namespace?

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

11. Does removing a field delete it from storage?

Yes; after removal the document is re‑saved without the field.

12. Can the journal be used for secure backups?

Yes.

13. Are null values allowed?

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

14. Are write operations immediately fsynced to disk?

No; writes are buffered and flushed after a short delay (default up to 60 seconds) unless the fsync option is used.

15. How does MongoDB handle transactions/locking?

MongoDB uses a lightweight, lock‑free design with automatic commit semantics similar to MySQL’s MyISAM; complex multi‑document transactions are not supported.

16. Why do data files become large?

MongoDB pre‑allocates space to reduce file‑system fragmentation.

17. How long does failover take?

Failover typically takes 10–30 seconds; writes fail during this window, but eventually consistent reads still succeed.

18. What is a master/primary?

The primary node in a replica set handles all write operations; if it fails, another node is elected.

19. What is a secondary/slave?

Secondaries replicate the oplog from the primary to stay in sync.

20. Must I call getLastError to ensure writes?

No; writes are applied regardless. getLastError only confirms acknowledgment.

21. Should I start with a sharded or unsharded cluster?

Begin with an unsharded deployment; you can add sharding later when data size grows.

22. How do sharding and replication work?

Each shard is a logical data partition; shards can be single servers or replica sets. Replication copies data across nodes for redundancy.

23. When does data get distributed across multiple shards?

MongoDB shards by range; when a collection exceeds the default chunk size (64 MB), additional chunks are created and distributed.

24. What happens when updating a document that is being moved between chunks?

The update is applied to the source shard first, then replicated to the destination before ownership transfer completes.

25. What if a shard is down or slow during a query?

If a shard is down, the query fails unless the "partial" option is set; if a shard is slow, MongoDB waits for its response.

26. Can old moveChunk files be deleted?

Yes; they are temporary and can be removed after balancing completes, but deletion must be done manually.

27. How to view MongoDB connection pool stats?

db._adminCommand("connPoolStats")

28. If moveChunk fails, must I manually clean up transferred documents?

No; the operation is deterministic and will retry until successful.

29. Can journaling be enabled for only part of a replica set?

Yes.

30. Updating a document during chunk migration

The update occurs on the source chunk first, then propagates to the destination before ownership transfer.

31. Does an index on {A:{B,C}} work for queries with {A:{C,B}} ?

No; the index matches the exact field order.

32. Query behavior when a shard is stopped or slow (duplicate of 25)

Same as above: errors unless "partial" is set; otherwise waits for response.

33. Does MongoDB support stored procedures?

Yes; JavaScript functions stored in db.system.js.

34. What is GridFS and why is it used?

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

35. Why are MongoDB data files large?

Because of pre‑allocation to avoid fragmentation.

36. Role of the analyzer (profiler) in MongoDB

Shows performance details of operations, helping locate slow queries.

37. How does MongoDB handle transactions/locking?

MongoDB is designed for lightweight, high‑performance operations without traditional locks or multi‑document rollbacks.

38. What does getLastError do?

It confirms whether a write operation was successfully committed.

39. MongoDB data structure

Data is stored as BSON (binary JSON) key‑value pairs.

40. Overall database hierarchy

Key‑value → Document → Collection → Database.

41. Language MongoDB is written in

MongoDB is implemented in C++.

42. Advantages of MongoDB

Document‑oriented storage (JSON‑like).

Index any attribute.

Replication and high scalability.

Automatic sharding.

Rich query capabilities.

Fast real‑time updates.

Professional support.

43. What is a collection?

A collection is a group of MongoDB documents, analogous to a table in relational databases.

44. What is a document?

A document is a set of key‑value pairs; schema is dynamic, so documents in the same collection can have different fields.

45. What is mongod ?

mongod

is the primary MongoDB server process handling data requests and background tasks.

46. mongod parameters

Data directory (default /data/db).

Port (default 27017).

47. What is mongo ?

mongo

is the command‑line shell used to connect to a mongod instance.

48. How to create a new database?

use myDatabase

Using use creates the database if it does not exist.

49. What is a non‑relational database?

Databases that do not use SQL or fixed table schemas, offering flexible design and high performance for big data and web apps.

50. Types of non‑relational databases

Key‑Value (e.g., Amazon S3)

Graph (e.g., Neo4j)

Document (e.g., MongoDB)

Column‑family (e.g., Cassandra)

51. Why use MongoDB?

Simple architecture

No complex joins

52. Typical MongoDB use cases

Big data

Content management systems

Mobile apps

General data management

53. What does a namespace mean in MongoDB?

Each collection and index has a namespace stored in a *.ns file (default 16 MB, ~24 000 namespaces). The file can be enlarged with --nssize up to 2 GB.

54. Languages with MongoDB drivers

C, C++, C#, Java, Node.js, Perl, PHP, etc.

55. How to list databases?

show dbs

56. What is sharding?

Sharding distributes data horizontally across multiple nodes to handle growth in data size and throughput.

57. What is replication?

Replication copies data to multiple servers, increasing availability and enabling disaster recovery.

58. How to insert a document into a collection?

db.collectionName.insert({"key":"value"})
db.collectionName.save({"key":"value"})

59. How to drop a database?

db.dropDatabase()

60. How to create a collection?

db.createCollection("collectionName")

61. How to list collections?

show collections

62. How to drop a collection?

db.CollectionName.drop()

63. Why use the profiler?

The profiler records command execution details into system.profile, a capped collection, helping diagnose performance issues.

64. Does MongoDB support primary/foreign key relationships?

MongoDB does not enforce foreign keys; relationships must be handled in application logic.

65. Supported data types

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

66. Why use the Code type?

To store JavaScript code within a document.

67. Why use the Regular Expression type?

To store regex patterns for query matching.

68. Why use the ObjectID type?

It provides a unique identifier for each document.

69. Components of an ObjectID

Timestamp, machine identifier, process identifier, and a 3‑byte incrementing counter.

70. What is an index?

Indexes improve query efficiency by storing a sorted representation of field values, avoiding full collection scans.

71. How to create an index?

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

72. How to format query results?

db.collection.find().pretty()

73. How to query with AND/OR conditions?

db.mycol.find({key1:value1, key2:value2}).pretty()   // AND
db.mycol.find({$or:[{key1:value1},{key2:value2}]}).pretty()   // OR

74. How to update data?

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

75. How to delete documents?

db.collection.remove({key:value}, true)   // true removes only one matching document

76. How to sort results?

db.collection.find({key:value}).sort({columnName:1})   // 1 = ascending, -1 = descending

77. What is aggregation?

Aggregation processes multiple documents and returns computed results, similar to SQL GROUP BY. Use db.collection.aggregate().

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

78. What is a replica set?

A replica set consists of one primary and multiple secondary nodes; writes go to the primary and are replicated to secondaries for redundancy.

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.

databaseshardingReplicationindexesMongoDBNoSQLaggregation
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.