mongoose
mongoose copied to clipboard
immutable with upsert in nested object
Hi,
I'm trying to use upsert and immutable feature and it doesn't seem to work in nested object. Is it supposed to work this way or am I doing it wrong?
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/upsert_immutable', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connection.dropDatabase();
const aInfo = {
a: String,
b: String,
c: String
}
const schema = new Schema({
name: String,
description: String,
xInfo: {
type: aInfo,
immutable: true,
required: true,
},
yInfo: {
type: String,
required: true,
},
zInfo: {
type: String,
immutable: true,
required: true,
},
})
const newDoc = {
name: 'name',
description: 'new desc',
xInfo: { a: 'a', b: 'b', c: 'c' },
yInfo: 'yInfo',
zInfo: 'zInfo'
}
const Model = mongoose.model('Test', schema);
await Model.findOneAndUpdate({ name: 'name' }, newDoc, { upsert: true });
console.log('Done', await Model.findOne(), new Date());
}
The result is
// notice that xInfo is not created but zInfo is
{
_id: 5fabaad551adf51882270695,
name: 'name',
__v: 0,
description: 'new desc',
yInfo: 'yInfo',
zInfo: 'zInfo'
}
node: v12.18.4 mongoose: 5.10.10 mongodb: 4.2.0
Any idea? Thanks!
This is a bug in Mongoose, thanks for reporting. Fix will be in v5.10.14.