MongooseError: document must have an _id before saving lib\model.js:297:18 at processTicksAndRejections (node:internal/process/task_queues:78:11)

Viewed 28

I got stuck at this mongoose code. I tried billions of time for this. but in my node app.js output is : MongooseError: document must have an _id before saving at D:\node_modules\mongoose\lib\model.js:297:18 at processTicksAndRejections (node:internal/process/task_queues:78:11)

and also data didn't save on mongoose shell. on some my research on internet i found one answer that if you dont provide '_id' then mongoose will do it automatically. On my code I didn't provide any _id. Now this didnt work also. If i tried with provide _id this also didnt work. What should I do now? Please, save my life!!!

Here is my code:

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/newDB", {useNewUrlParser: true});

const peopleSchema = new mongoose.Schema({
    name: String,
    age: Number,
    address: String
});
const People= mongoose.model("People", peopleSchema);
const person = new People(
    {
        name: "john",
        age: 25,
        address: "New York, USA"
},
{ 
    name: "Don",
    age: 30,
    address: "Chicago, USA"
},
{
    name: "Kahn",
    age: 34,
    address: "Dhaka, Bangladesh"
}
);
person.save();
3 Answers

The new People() constructor is when you are adding 1 new document.

If you want to add multiple documents the command is:

await People.insertMany({
    name: "john",
    age: 25,
    address: "New York, USA"
},
{ 
    name: "Don",
    age: 30,
    address: "Chicago, USA"
},
{
    name: "Kahn",
    age: 34,
    address: "Dhaka, Bangladesh"
})

Note: im using the promises so you will need async at the top of teh function, if you want to provide a callback add .exec() after the updateMany

Mongoose's document instance can only save one document per time. If you want to add multiple documents then you'll have to rely on MongoDb's native insertMany operation.

const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/newDB', { useNewUrlParser: true })

const peopleSchema = new mongoose.Schema({
  name: String,
  age: Number,
  address: String,
})
const People = mongoose.model('People', peopleSchema)
await People.insertMany([
  {
    name: 'john',
    age: 25,
    address: 'New York, USA',
  },
  {
    name: 'Don',
    age: 30,
    address: 'Chicago, USA',
  },
  {
    name: 'Kahn',
    age: 34,
    address: 'Dhaka, Bangladesh',
  },
])

or with callback approach...

const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/newDB', { useNewUrlParser: true })

const peopleSchema = new mongoose.Schema({
  name: String,
  age: Number,
  address: String,
})
const People = mongoose.model('People', peopleSchema)
People.insertMany([
  {
    name: 'john',
    age: 25,
    address: 'New York, USA',
  },
  {
    name: 'Don',
    age: 30,
    address: 'Chicago, USA',
  },
  {
    name: 'Kahn',
    age: 34,
    address: 'Dhaka, Bangladesh',
  },
], (error, docs) => console.log(docs))

You have to use insertMany() to save many document in one time. You can try this code:

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/newDB", {useNewUrlParser: true});

const peopleSchema = new mongoose.Schema({
    name: String,
    age: Number,
    address: String
});
const People= mongoose.model("People", peopleSchema);
const person = [
    {
        name: "john",
        age: 25,
        address: "New York, USA"
    },
    { 
    name: "Don",
    age: 30,
    address: "Chicago, USA"
    },
    {
    name: "Kahn",
    age: 34,
    address: "Dhaka, Bangladesh"
    }
];
People.insertMany(person, async(err, docs) {
    await docs.save();
});

You can read more about this in here: https://mongoosejs.com/docs/api/model.html#model_Model-insertMany

Related