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

148
Views
Model.create() from Mongoose doesn´t save the Documents in my Collection

I have created a sigle app with a Schema and a Model to create a Collection and insert some Documents.

I have my todoModel.js file:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const todoSchema = new Schema({
username: String,
todo: String,
isDone: Boolean,
hasAttachment: Boolean
});
const Todos = mongoose.model("Todo", todoSchema);
module.exports = Todos;

Then I have created a setUpController.js file with a sample of my Documents. Then I create a Model and I pass my sample of Documents and my Schema. I create a response to send tje result in JSON. Everything good here, as I get the result in json when accessing to the route.

Here is the code:

        Todos.create(sampleTodos, (err, results) => {

        if (!err) {
            console.log("setupTodos sample CREATED!")
            res.send(results);
        }
        else {
            console.log(`Could not create the setupTodos Database sample, err: ${err}`);
        }
        });

My problem is that this Documents don´t get saved in the collection !! When I access to the database, nothing is there.

This is my app.js file:

mongoose.connect("mongodb://localhost:27017/nodeTodo")
.then(connection => {
    app.listen(port);
    
})
.catch(err => {
    console.log(`Could not establish Connection with err: ${err}`);
});

Could anyone help me please ?

Thank you

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can't use object directly without making an instance of it. Try creating an instance and make the respective function call of that instance.In your case, save the document after creating an instance and it works like a charm.

const newTodos = new Todos({
username: "username",
todo: "todos",
isDone: false,
hasAttachment: flase
});

const createdTodo = newTodos.save((err, todo) => {
if(err) {
throw(err);
}
else {
//do your staff
}
})

about 4 years ago · Juan Pablo Isaza Report

0

after the collection is created you can use the function inserMany to insert also a single document the function receives an array of objects and automatically saves it to the given collection

example:

  Pet = new mongoose.model("pet",schemas.petSchema)
  Pet.insetMany([
    {
      //your document
    }])

it will save only one hardcoded document

I hope it was helpful

about 4 years ago · Juan Pablo Isaza 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!