Search code examples
node.jsmongodbmongoose

I want to implement a simple update in my db but I am getting this { ok: 0, n: 0, nModified: 0 } in my console?


# I can't update keep getting { ok: 0, n: 0, nModified: 0 } ! # ... const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mongo-exercises', {useNewUrlParser: true, useUnifiedTopology: true }) ... .then(() => console.log('Connected to MongoDB... ')) .catch(() => console.error('Could not connect to MongoDB...', err));

...

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


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

 async function updateCourse(id){
    const result = await Course.updateOne({_id: id}, {
        $set: {
            author: 'Momo',
            isPublished: true
        }
    });
...

    console.log(result);    //{ n: 0, nModified: 0, ok: 1 }

}

...

 updateCourse('5a68ff090c553064a218a547');

Blockquote


Solution

  • I got it.. I did import the data to my database from a json file and the id didn't have an ObjectId and that doesn't work with the newer version of mongoose.

    so the id was like this: "_id":"5a68fdc3615eda645bc6bdec"

    and this the right way for mongoose 5.9.18 : "_id": ObjectId("5a68fdc3615eda645bc6bdec")