bulkWrite UpdateOne fails handling sub-documents while the updateOne() and findOneAndUpdate() funcitons work
See original GitHub issueDo you want to request a feature or report a bug? A Bug
What is the current behavior? The current behavior is bizzare. I have a model that has an array of schema that I have created. I am attempting to do a bulk update which runs without error, but doesn’t actually update anything. With the same filter and update ‘$set’ command, the update works when not using bulk write. updateOne() and findOneAndUpdate() both work.
If the current behavior is a bug, please provide the steps to reproduce. Here are the schema and models:
const mongoose = require('mongoose');
const SectionInfoSchema = require('./section-info');
const SubjectInfoSchema = mongoose.Schema({
code: { type: String, requried: true, index: true },
subjectTitle: { type: String, requried: true },
courses: { type: [SectionInfoSchema], requried: true }
});
module.exports = mongoose.model('subject-info', SubjectInfoSchema);`
Here is the schema for SectionInfo:
const mongoose = require('mongoose');
const SectionInfoSchema = new mongoose.Schema({
courseid: { type: String, required: true, index: true },
crn: { type: String, required: true },
profesorName: { type: String, required: true },
subjectTitle: { type: String, required: true },
sectionNumber: { type: String, required: true },
seatsAvailable: { type: Number, required: true },
creditHours: { type: Number, required: true },
courseType: { type: String, required: true },
meetingInfo: { type: Object, required: true }
});
module.exports = SectionInfoSchema;`
I run a script to webscrape some course info from my school (its public data). I then want to update this in my database with a bulk write. For reference coursesToBeUpdated is an array of objects that have a unique course id and the new data for the seats to be updated. Here is the code for setting up that bulkWrite:
var bulkOpsSection = []; // Bulk operations for the section
var bulkOpsSubjectInfo = []; // Bulk operations for the subject infos
for(var i = 0; i < coursesToBeUpdated.length; i++) {
// Section update doc
let updateDoc = {
'updateOne': {
'filter': { 'courseid': coursesToBeUpdated[i].id },
'update': { '$set': { 'seatsAvailable': coursesToBeUpdated[i].seatsAvailable } }
}
};
// Subject Info update doc
let thatOtherUpdateDoc = {
'updateOne': {
'filter': {'courses.courseid': coursesToBeUpdated[i].id},
'update': { '$set':
{
'courses.$.seatsAvailable': coursesToBeUpdated[i].seatsAvailable
}
}
}
};
// push the docs to the update queus
bulkOpsSection.push(updateDoc);
bulkOpsSubjectInfo.push(thatOtherUpdateDoc);
};
// update the section info
await Section.collection.bulkWrite(bulkOpsSection)
.then( bulkWriteOpResult => {
console.log('BULK update OK');
console.log(JSON.stringify(bulkWriteOpResult, null, 2));
})
.catch( err => {
console.log('BULK update error');
console.log(JSON.stringify(err, null, 2));
});
// update the subjectInfo
await SubjectInfo.collection.bulkWrite(bulkOpsSubjectInfo)
.then( bulkWriteOpResult => {
console.log('BULK update OK');
console.log(JSON.stringify(bulkWriteOpResult, null, 2));
})
.catch( err => {
console.log('BULK update error');
console.log(JSON.stringify(err, null, 2));
});
The bulkwrite on the section collection works fine. However, when adding the update operator ‘$’, the issue starts occurring where no error happens but nothing is updated. That same filter and update object works in the updateOne() function and findOneAndUpdate() function just not in this bulkWrite form.
What is the expected behavior? The expected behavior is to update each collection with the new and updated amount of seats available for the course. updateOne as it is seen being used as added to the list of docs for the bulkWrite should go through, find the document with that courseid, then use that found index to update the seats. It is not doing this.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node.js: 12.18.3 Mongoose: 5.13.2 MongoDB:
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
reportee claims no error but I am getting the error block to execute when I run this example.
Quick side note: the error object in this version of mongoose returns {“driver”:true, “name”:“MongoError”} but in 6.X its an empty object
https://mongoosejs.com/docs/api/model.html#model_Model-collection