Introduction
Understanding the difference between rows and documents helps you organize data the right way. It makes working with databases easier and faster.
Jump into concepts and practice - no test required
Understanding the difference between rows and documents helps you organize data the right way. It makes working with databases easier and faster.
SQL row example: (id, name, age) (1, 'Alice', 30) MongoDB document example: { _id: ObjectId(...), name: 'Alice', age: 30 }
SQL row:
SELECT * FROM users WHERE id = 1;MongoDB document: db.users.findOne({ _id: ObjectId('507f1f77bcf86cd799439011') });
SQL row insert: INSERT INTO users (id, name, age) VALUES (2, 'Bob', 25);
MongoDB document insert: db.users.insertOne({ name: 'Bob', age: 25 });
This example shows how to add a document with flexible fields and then find it by name.
use testdb; // Insert a document in MongoDB db.users.insertOne({ name: 'Alice', age: 30, hobbies: ['reading', 'hiking'] }); // Find the document db.users.find({ name: 'Alice' }).pretty();
Rows have fixed columns; documents can have different fields in each record.
Documents store related data together, which can reduce the need for joins.
Think of rows like spreadsheet rows, and documents like folders with files inside.
Rows are simple and fixed; documents are flexible and nested.
Use rows for structured, uniform data; use documents for varied, complex data.
Understanding this helps you pick the right database and design your data well.
users with documents like {name: 'Bob', scores: [10, 20, 30]}, what will db.users.find({scores: 20}) return?db.orders.find({items: {product: 'Book'}}) but get no results. What is the likely problem?