This is some sample Mongo DB code in a simple Rest API, which has basic CRUD endpoints to refer to as a guide.
To run this sample code, install the npm packages & run the server.js file.
npm install
node server
First define a 'model' of the document you are going to store in the database, in the form of JSON in a JS file (Post.js
in our case). This model is the structure of the document to be stored in the database.
const mongoose = require("mongoose");
const PostSchema = mongoose.Schema({
title: {
type: String,
required: true,
},
desc: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Posts", PostSchema);
Note that we are using mongoose
library to interact with Mongo DB.
To import the 'Post' model use the following code.
const Post = require("./models/Post");
Next, we'll connect with Mongo DB using the credentials provided when creating a cluster (database).
// Connecting to the database
const connectDB = async () => {
try {
await mongoose.connect(process.env.DB_CONN, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("MongoDB Connected!");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
The following function returns all the records in the Posts model
await Post.find();
To get a record by Id
, just pass the Id
in the findById()
function.
await Post.findById(req.params.id);
Add a new Post by passing the data as per the model defined earlier. Note that post.save()
function has to be called whenever a new post
is to be saved to the database.
const post = new Post({
title: req.body.title,
desc: req.body.desc,
});
const savedPost = await post.save();
To update a post, first you need to pass in the Id
by which the post is identified and then it is updated by using the $set
flag.
await Post.updateOne(
{ _id: req.body.id },
{
$set: {
desc: req.body.desc,
},
}
);
To delete a post, simply pass the Id
to the remove()
function.
await Post.remove({ _id: req.params.id });