Mongoose model.save is slower if schema has sub-document or sob-document array

37 views
Skip to first unread message

Nilesh Maheshwari

unread,
Dec 21, 2017, 3:03:56 PM12/21/17
to mongodb-user
I need some clarification on why model.save is slower if schema has sub-document or sob-document array.
I am using Mongoose v4.9.4

var Person = new mongoose.Schema({
    Name: {
            type: String
    },
    Addresses: [{
        streetaddress1: {
            type: String
        },
        streetaddress1: {
            type: String
        },
        city: {
            type: Number
        }
    }]
});


Person.save takes more time if Addresses has 10000 entries.


Whereas below model.save with nested schema takes 1/100th of time with below nested schema.


var AddressSchema = new mongoose.Schema({
        streetaddress1: {
            type: String
        },
        streetaddress1: {
            type: String
        },
        city: {
            type: Number
        }
    });

var Person = new mongoose.Schema({
Name: {
            type: String
    },
    Addresses: [AddressSchema]
});


Really appreciate you help on this one.

Wan Bachtiar

unread,
Jan 10, 2018, 7:12:47 PM1/10/18
to mongodb-user
Note: This question has been answered on https://github.com/Automattic/mongoose/issues/5925 

Reply all
Reply to author
Forward
0 new messages