简体   繁体   中英

What is the best mongoose practices to PATCH a large document: global or specific PATCH?

Let say I have a large Model like this:

const largeSchema = mongoose.Schema({

    var1: {type: Number, required: true, unique: true},
    var2: {type: String, required: true},
    var3: {type: String, required: true},
    var4: {type: String, required: true},
    varsA: {type: [String], required: true, default: []},
    varsB: {type: [SchemaB], ref: 'SchemaB'},
    var5: {type: mongoose.Schema.Types.ObjectId, ref: 'Schema5'},
    varsC: {type: [SchemaC], required: true, default: []},
    varsD: {type: [SchemaD], required: true, default: []},
    varsE: {type: [SchemaE], required: true, default: []},
    varsF: {type: [SchemaF], required: true, default: []},
    var6 {type: Schema6},
    varsG: {type: [SchemaG], required: true, default: []},
    varsH: {type: [SchemaH], required: true, default: []},
    
  
},{timestamps: true});

As you can see, model like this one contains each referenced and embedded models, some in array some not. Furthermore some of these array can be large (no more than 100 items per arrays).

My question is about these embedded schemas in array.

Let's say that every fields can be updated. If I want to update one item in varsE . Should I use a Global PATCH that will patch my whole list; or build one route for each array of this model?

Architecture: Node.JS - Express - Mongoose

I would suggest you update only the specific element in the array instead of a global patch in order to avoid unnecessary contamination of other elements. Following is an example of how you could update a specific element in the array.

 await LargeSchemaModel.update({'items.id': 1}, {'$set': { 'items.$.name': 'updated name', 'items.$.value': 'updated value' }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM