Loading lesson path
Node.js
Database Integration focused on Node.js MySQL and related concepts.
Node.js MySQL
Creating a Database To create a database in MySQL, use the "CREATE DATABASE" statement: Example Create a database named "mydb": let mysql = require('mysql'); let con = mysql.createConnection({ host:…
Creating a Table To create a table in MySQL, use the "CREATE TABLE" statement. Make sure you define the name of the database when you create the connection: Example Create a table named "customers":…
Insert Into Table To fill a table in MySQL, use the "INSERT INTO" statement. Example Insert a record in the "customers" table: let mysql = require('mysql'); let con = mysql.createConnection({ host: "…
Selecting From a Table To select data from a table in MySQL, use the "SELECT" statement. Example Select all records from the "customers" table, and display the result object: let mysql = require('mys…
Select With a Filter When selecting records from a table, you can filter the selection by using the "WHERE" statement: Example Select record(s) with the address "Park Lane 38": let mysql = require('m…
Sort the Result Use the ORDER BY statement to sort the result in ascending or descending order. The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use…
Delete Record You can delete records from an existing table by using the "DELETE FROM" statement: Example Delete any record with the address "Mountain 21": let mysql = require('mysql'); let con = mys…
Delete a Table You can delete an existing table by using the "DROP TABLE" statement: Example Delete the table "customers": let mysql = require('mysql'); let con = mysql.createConnection({ host: "loca…
Update Table You can update existing records in a table by using the "UPDATE" statement: Example Overwrite the address column from "Valley 345" to "Canyon 123": let mysql = require('mysql'); let con…
Limit the Result You can limit the number of records returned from the query, by using the "LIMIT" statement: Example Select the 5 first records in the "customers" table: let mysql = require('mysql')…
Join Two or More Tables You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement. Consider you have a "users" table and a "products" table: user…
Node.js can be used in database applications. One of the most popular NoSQL database is MongoDB. MongoDB To be able to experiment with the code examples, you will need access to a MongoDB database. Y…
Creating a Database To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create.…
A collection in MongoDB is the same as a table in MySQL Creating a Collection To create a collection in MongoDB, use the createCollection() method: Example Create a collection called "customers": let…
Insert Into Collection To insert a record, or document as it is called in MongoDB, into a collection, we use the insertOne() method. A document in MongoDB is the same as a record in MySQL The first p…
In MongoDB we use the find and findOne methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database. Find One To select data from a collect…
Filter the Result When finding documents in a collection, you can filter the result by using a query object. The first argument of the find() method is a query object, and is used to limit the search…
Sort the Result Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter, an object defining the sorting order. Example Sort the result alphabe…
Delete Document To delete a record, or document as it is called in MongoDB, we use the deleteOne() method. The first parameter of the deleteOne() method is a query object defining which document to d…
Drop Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. The drop() method takes a callback function containing the error object and the result pa…
Update Document You can update a record, or document as it is called in MongoDB, by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which docume…
Limit the Result To limit the result in MongoDB, we use the limit() method. The limit() method takes one parameter, a number defining how many documents to return. Consider you have a "customers" col…
Join Collections MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the…