Reputation: 51
so I'm trying to update a document but it just doesn't work with _id but if I filter with "name" it works I also tried with FindByID and it returns null using mongoose version 5.0.18
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises')
.then(() => console.log('connected to database :)'))
.catch(reason => console.log('Can not connect to data base',reason));
const courseSchema = mongoose.Schema({
name: String,
author: String,
tags: [String],
Date: { type:Date,default:Date.now },
isPublished: Boolean,
price: Number
});
const Course = mongoose.model('courses',courseSchema);
async function updateCourse(id){
let result = await Course.update({_id: id},{
$set:{
author: "some random dude",
isPublished: true
}
});
console.log('result: ',result);
}
updateCourse('5a68fde3f09ad7646ddec17e');
Upvotes: 1
Views: 245
Reputation: 51
I just had to re-import the collection without _id so it can regenerate them
Upvotes: 0
Reputation: 85
try using
//make sure you import Course module
Course.findByIdAndUpdate(id)
.then(response=>{
})
.catch(err=>{
})
Upvotes: 1