Code Monkey home page Code Monkey logo

nodejs-mongo-backend's Introduction

Node.js MongoDB back-end basic demo app.

Basic nodejs back-end that exposes CRUD functionality and queries to a MongoDB collection using the library devextreme-query-mongodb.

1. Database server setup

Option 1: VirtualBox Vagrant setup

  • 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.

Option 2: Mongo DB local installation

Click to expand!

Install Nodejs

Todo: Document how to install Nodejs

Install MongoDB

Ubuntu

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/.

Windows

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

Start mongoDB as a replica

Stop any mongodb server instance.

Create data directory and Start a replica instance in the default port 27017

Run the following commands to set up a simple, single-node replica set (for testing purposes).

Windows

mkdir C:\mongodb\data
mongod --replSet rs0 --dbpath "C:\mongodb\data"

Ubuntu

mkdir -p /mongodb/data
mongod --replSet rs0 --dbpath /mongodb/data

Connect (in a different terminal) using mongosh and initialize the replica

mongosh mongodb://<MONGODB_SERVER_IP>:27017/test
rs.initiate()

Import test collection

Import the restaurants collection into the test database as shown in this readme file: https://github.com/mongodb/docs-assets/tree/drivers

2. Run the project

Once the mongodb server is running you can continue and run this project

From the Vagrant virtual machine

The Nodejs server is installed and automatically started within the Vagrant virtual machine. You should be able to access at:
http://localhost:8080/frontend

The following endpoints are available for testing:

  1. GET /grades: this endpoint uses the library devextreme-query-mongodb to format request/response parameters. So it can be used to test DevExtreme components.

  2. (GET, POST, PUT, PATCH, DELETE) /api/grades: this endpoint exposes a basic CRUD functionality.

  3. 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.

  4. 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.

From a local Nodejs installation

You can start the nodejs server by running:
node app

3. Update the collection using mongosh

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);
}

References

nodejs-mongo-backend's People

Contributors

dxhereira avatar solaechea avatar

Watchers

 avatar

Forkers

dxhereira

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.