Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Make post request to Mongodb Atlas using Nodejs

I was familiar with MongodB for CRUD operation. Here, I'm trying to make simple post request on mongodB atlas but I want to know where I have done error for the connection and posting data to MongodB atlas.

Model.js

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

let quizSchema = new Schema({
  title: {
    type: String,
  },
  description: {
    type: Number,
  },

  question: {
    type: String,
  },
});

const Quiz = mongoose.model("Quiz", quizSchema);
module.exports = Quiz;

index.js

I'm trying to create the database collection name "QuizDatabase" and insert the data to it.

var express = require("express");
var bodyParser = require("body-parser");
const Quiz = require("./views/model/model");

var Request = require("request");
var app = express();
app.use(bodyParser.json());


const MongoClient = require("mongodb").MongoClient;
const uri =
  "mongodb+srv://username:password@cluster0.iom1t.mongodb.net/QuizDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
mongoose.connect(uri);


var server = app.listen(process.env.PORT || 8080, function () {
  var port = server.address().port;
  console.log("App now running on port", port);
});

app.post("/new/", function (req, res) {
  Quiz.collection("QuizDatabase").insertMany(req.body, function (err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new quiz.");
    } else {
      res.status(201).send(JSON.stringify(body));
    }
  });
});
  function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({ error: message });
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You dont have to use mongo client if you are already using mongoose. In index.js file just import the model

const Quiz = require("./model");

And you are already using mongoose to connect to db when you write mongoose.connect(uri); You don't have to use client.connect() again.

Query to insert -

Quiz.insertMany(req.body);

Your index file should look like this -

const Quiz = require("./views/model/model");

var Request = require("request");
var app = express();
app.use(bodyParser.json());


const uri =
"mongodb+srv://username:password@cluster0.iom1t.mongodb.net/QuizDatabase?retryWrites=true&w=majority";

mongoose.connect(uri);


var server = app.listen(process.env.PORT || 8080, function () {
  var port = server.address().port;
  console.log("App now running on port", port);
});

app.post("/new/", function (req, res) {
  Quiz.insertMany(req.body, function (err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new quiz.");
    } else {
      res.status(201).send(JSON.stringify(body));
    }
  });
});
  function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({ error: message });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!