簡體   English   中英

在Mongoose / MongoDB中,如何更新數組的數組中的屬性

[英]In Mongoose/MongoDB, how to update properties in an array of an array

我有以下模型:

訂單模型:

var order = new Schema({
content: [{
    product: {type: String, required: true},
    quantity: {type: Number},
    vatRate: {type: Number, required: true},
    price: {type: Number},
    deviceId: {type: String}
    }],
number: {type: Number},
tableId: {type: Schema.ObjectId, ref: 'Table'}
isAutomated: {type: Boolean, default: true},
createdAt: {type: Date, default: Date.now}
});

表模型:

var table = {
tableNumber: {type: Number, default: 0},
status: {type: String, enum: ['Booked', 'Free', 'Active'], default: 'Free'},
order: [order]
};

設備型號:

var device = {
    deviceId: {type: String, required: true},
    status: {type: String, enum: ['Booked', 'Free', 'Active'], default:   'Free'},
    token: {type: String},
    tableId: {type: Schema.ObjectId, ref: 'Table'},
    isEnabled: {type: Boolean, default: true}
};

客戶模型:

var client = new Schema({
    information: {
        code: {type: String, required: true},
        name: {type: String, required: true}
},
account: {
    username: {type: String, required: true},
    password: {type: String, required: true},
    token: {type: String}
},
devices: [device],
tables: [table],
products: [product],
notifications: [notification],
createdAt: {type: Date, default: Date.now}
});

一個客戶端有多個表。 一個表有多個訂單。

如何更新訂單數組中的訂單,訂單本身又是表格中的數組?

在我的代碼的另一部分中,我知道當只有一個級別的數組時該怎么做……像這樣:

 clientModel.findOneAndUpdate(
    {
        "information.code": clientCode,
        'devices.deviceId': deviceId
    },
    {
        $set: {
            'devices.$.status': device.status,
            'devices.$.tableId': device.tableId
        }
    },
    {
        new: true,
        select: {
            devices: {
                $elemMatch: {deviceId: deviceId}
            }
        }
    },

但是如何使用嵌套數組呢?

謝謝你的幫助,

帕特里斯

使用$ push運算符更新數組條目。 嵌套數組可以使用'。'訪問。 此處解釋的符號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM