What Is MongoDB
MongoDB is a database that stores data as documents. A document is a set of named fields, like one note.
A document
This object is one note, written as a document.
const note = { title: 'Buy milk', done: false };
console.log(note.title);
console.log(note.done);A collection
This array holds three documents, like one collection.
const notes = ['Buy milk', 'Ship MERN', 'Read a book'];
console.log(notes.length);Example
const notes = [
{ title: 'Buy milk', done: false },
{ title: 'Ship MERN', done: true },
{ title: 'Read a book', done: false }
];
for (const note of notes) {
console.log(note.title);
}Key points
- A database is a program that stores data and gives it back on request.
- A document is one record, such as one note.
- A collection is a group of documents of the same kind.
- Each field holds a single named value.
- MongoDB stores documents in a format close to JSON.
In this example, notes is an array of three documents. Each document has a title field and a done field. The loop prints the title of each document. The output lists the three notes the course uses.
Note
MongoDB has no tables and no rows. It has collections and documents.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What is a collection in MongoDB?