Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Executing replaceOne with a subdocument array of strings results in saving an array of null items instead.
If the current behavior is a bug, please provide the steps to reproduce.
Steps to reproduce:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/bug-reports', {useNewUrlParser: true});
const key = 'A';
const Schema = new mongoose.Schema({
key: {
type: String
},
items: {
type: [String],
default: []
}
}, {
collection: 'report-181016'
});
const Record = mongoose.model('Record', Schema);
const record = {
key,
items: ['A', 'B', 'C']
};
(async () => {
try {
const result = await Record.replaceOne({key}, record, {upsert: true});
console.log('Replace one result', result);
const fetchedRecord = await Record.findOne({key}).exec();
console.log('Fetched record', fetchedRecord);
process.exit(0);
} catch (err) {
console.log('Err', err);
process.exit(1);
}
})();
Result:
Replace one result { n: 1, nModified: 1, ok: 1 }
Fetched record { items: [ null, null, null ],
_id: 5bc637dbe6188e82a318ba00,
key: 'A' }
What is the expected behavior?
replaceOne should save the subdocument array of strings correctly.
Please mention your node.js, mongoose and MongoDB version.
node: 8.9.4
mongoose: 5.3.4
mongodb: 3.6.8
Hi, is your schema correct
items: [{
type: String,
default: []
}].
I just tried my test code with the schemas as you suggested, @santhosh77h, but it results in the same bug.
Thanks for reporting, will fix ASAP :+1:
Thank you for your quick fix!