Mongoose: replaceOne saves subdocument array of strings as null values instead

Created on 16 Oct 2018  路  4Comments  路  Source: Automattic/mongoose

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

confirmed-bug

All 4 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Igorpollo picture Igorpollo  路  3Comments

gustavomanolo picture gustavomanolo  路  3Comments

ghost picture ghost  路  3Comments

lukasz-zak picture lukasz-zak  路  3Comments

p3x-robot picture p3x-robot  路  3Comments