Learning Node.js MongoDB
- Part 1: Terminology and Comparison with SQL
- Part 2: Connecting and Creating
- Part 3: Reading
- Part 4: Updating
- Part 5: Deleting
MongoDB is a popular NoSQL document-oriented database system. The Node.js driver allows for connecting to an existing MongoDB installation or system.
For many years, the most popular database management systems were those based on the Structured Query Language (SQL). These were designed for operating on data sizes of dozens of records to hundreds of tables. However, with the rise of massive web applications and a need for faster overall operations, many SQL systems were not able to keep up with developmental needs.
Increasingly, a classification of systems named NoSQL have become popular. Instead of being based on relational models, these are based on others such as the document model where data is organized by documents in key-value relationships, which can return and process data across some common operations faster than the same operations in a relational database management system (RDBMS).
MongoDB organizes data across different layers and concepts using JSON-like structures based on BSON (binary-encoded JSON).
Database: A container for collections. Most MongoDB installations have multiple databases.
Collection: A group of documents. Conceptually similar to a table in SQL.
Document: A group of fields. Conceptually similar to a row in a SQL.
Field: A key-value pair. Conceptually similar to a column in a SQL.
Comparison with SQL Functionality
|Create||insertOne(), insertMany()||INSERT INTO|
|Read||findOne(), find()||SELECT… FROM|
|Update||updateOne(), updateMany()||UPDATE.. WHERE|
|Delete||deleteOne(), deleteMany()||DELETE FROM|
Working with MongoDB in Node.js takes place via callback functions. Based on specifying the database, collection, and then operation, the first argument passed to the callback function will always be an error value with the second the requested collection, document, or field.