Basic nodejs back-end that exposes CRUD functionality and queries to a MongoDB collection using the library devextreme-query-mongodb.
- Download and install VirtualBox.
- Download and install Vagrant.
- Open a command line and change directory to the base path of this project.
- Execute the
vagrant up
command, once the process completes an Ubuntu-MongoDB virtual machine will be running. - Continue to step 2.
Click to expand!
Todo: Document how to install Nodejs
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/.
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/.
install mongosh as a separate package.
Install MongoDB tools (mongoimport).
Add the path to mongoDB files to windows PATH
In my case it was
C:\Program Files\MongoDB\Server\5.0\bin\
C:\Program Files\MongoDB\Tools\100\bin\
It wasn't required to add mongosh app to the windows PATH variable, but it was installed on this directory:
C:\Users\HP\AppData\Local\Programs\mongosh
Stop any mongodb server instance.
Run the following commands to set up a simple, single-node replica set (for testing purposes).
mkdir C:\mongodb\data
mongod --replSet rs0 --dbpath "C:\mongodb\data"
mkdir -p /mongodb/data
mongod --replSet rs0 --dbpath /mongodb/data
mongosh mongodb://<MONGODB_SERVER_IP>:27017/test
rs.initiate()
Import the restaurants collection into the test database as shown in this readme file: https://github.com/mongodb/docs-assets/tree/drivers
Once the mongodb server is running you can continue and run this project
The Nodejs server is installed and automatically started within the Vagrant virtual machine. You should be able to access at:
http://localhost:8080/frontend
-
GET /grades: this endpoint uses the library devextreme-query-mongodb to format request/response parameters. So it can be used to test DevExtreme components.
-
(GET, POST, PUT, PATCH, DELETE) /api/grades: this endpoint exposes a basic CRUD functionality.
-
GET /grades/stream: This endpoint uses mongo Change Streams to respond with a Server-sent event when any operation (insert, update, delete) is performed on the Grades collection.
-
GET /frontend: This is an endpoint for testing the back-end endpoints. It responds with an HTML page that displays a list of Grades and dynamically updates the table when a notification (Server-sent event) is received.
You can start the nodejs server by running:
node app
Connect to mongosh
mongosh mongodb://localhost:27017/test
Then within mongosh To update a single document:
db.grades.updateOne(
{ quizScore: { $gte: 90 } },
[{ $set: { examScore: { $round: [ { $multiply: [ { $rand: {} }, 100 ] }, 2 ] } } }]
);
To update multiple documents:
try {
db.grades.updateMany(
{ examScore: { $lte: 25 } },
[{ $set: { examScore: { $round: [ { $multiply: [ { $rand: {} }, 100 ] }, 2 ] } } }],
);
} catch (e) {
print(e);
}