简体   繁体   English

PATCH 大型文档的最佳 mongoose 做法是什么:全局 PATCH 还是特定 PATCH?

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

Let say I have a large Model like this:假设我有一个像这样的大 Model:

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.如您所见,像这样的 model 包含每个引用模型和嵌入式模型,有些在数组中,有些则没有。 Furthermore some of these array can be large (no more than 100 items per arrays).此外,其中一些数组可能很大(每个数组不超过 100 个项目)。

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 .如果我想更新varsE中的一项。 Should I use a Global PATCH that will patch my whole list;我应该使用全局补丁来修补我的整个列表吗? or build one route for each array of this model?或者为这个 model 的每个数组构建一条路由?

Architecture: Node.JS - Express - Mongoose架构: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' }});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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