1

I am trying to add multiple in createCourse() method, but in mongoDB compass it adds a array in MongoDB compass. The output of the mongodb compass gives an objet containing _id, tags, data and __v. How to handle this?

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log('Connected with MongoDB...'))
    .catch(err => console.log("Couldn't connect with MongoDB!!!", err));

const courseSchema = new mongoose.Schema({
    name: String,
    author: String,
    edition: Number,
    tags: [ String ],
    data: {type: Date, default: Date.now},
     isPublished: Boolean
})

const Course = mongoose.model('Course', courseSchema);

async function createCourse() {
    const course = new Course([{
        name: "Node Course",
        author: "Ammar Siddiqi",
        edition: 3,
        tags: ['node', 'backend', 'server'],
        isPublished: true
    },{
        name: "React Course",
        author: "Mosh",
        edition: 3,
        tags: ['Functionalties', 'frontend', 'server'],
        isPublished: true
    }]);
    const result = await course.save();
    console.log(result);
}

createCourse();```

Ammar Siddiqi
  • 35
  • 1
  • 3
  • 2
    This may helpful to you: https://stackoverflow.com/questions/10266512/how-can-i-save-multiple-documents-concurrently-in-mongoose-node-js#:~:text=Mongoose%204.4%20added%20a%20method%20called%20insertMany – Mani Nov 28 '21 at 07:51

0 Answers0