JS GUI for Simple CRUD

We will use stitch.js to build up the CRUD sample, it is provided by MongoDB.Stitch.

Please login MongoDB.stitch to play with Users CRUD sample.

On this sample page, you can:

  • Connect to the database
  • List all users from collection
  • Get total count from collection
  • Add a new user
  • Find a user by _ID
  • Update a user
  • Delete a user

You can find the related JavaScript codes below for the above features:

Features JavaScript Codes
Connect to the database // Define Application ID here
const client = stitch.Stitch.initializeDefaultAppClient(“it-blogs-vrzsk“);                                             

// Setup MongoDB Service Client const mongodb = client.getServiceClient(               

// Define Database Name here
const db = mongodb.db(“blog“);

// Connect and Login to the database
client.auth.loginWithCredential(new stitch.AnonymousCredential()).catch(console.error);  
List all users from collection   db.collection(“users“).find({}, {limit: 1000}).asArray()
Get total count from collection   db.collection(“users“).count()
Add a new user   db.collection(“users“).insertOne({ owner_id : client.auth.user.id, username: “Peter Pan”, status: “Active” })  
Find a user by _ID db.collection(“users“).find( {“_id”:{“$oid”:id}} , {limit: 1000}).asArray()   _id field is an auto-created unique field when you create the collection.  
Update a user   db.collection(“users“).updateOne( {“_id”:{“$oid”:id}}, { $set: { username: ”Mary”, status: “Inactive” } } )  
Delete a user   db.collection(table).deleteOne( {“_id”:{“$oid”:id}} )

Let’s play with the sample, you will find that it is quite simple to create a small web micro-service with MongoDB.

Please rate this