【发布时间】:2015-09-09 09:16:28
【问题描述】:
我在发票模式中有一个嵌套项目模式,如下所示:
var InvoiceSchema = new db.Schema({
title: {
type: String,
required: true
},
description: String,
clientId: db.Schema.ObjectId,
companyId: db.Schema.ObjectId,
poNumber: Number,
invoiceNumber: {
type: Number,
default: 1000
},
taxApplied: {
type: Boolean,
default: false
},
feeApplied: {
type: Boolean,
default: false
},
lastUpdated: {
type: Date,
default: Date.now
},
createdOn: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: ['presented', 'entered', 'hold', 'paid',
'partially paid', 'reversed', 'void'],
required: true
},
invoiceItems: [InvoiceItemSchema]
});
var InvoiceItemSchema = new db.Schema({
invoiceId: {
type: db.Schema.ObjectId,
required: true
},
description: String,
qty: Number,
rate: Number,
isFlatFee: {
type: Boolean,
default: false
}
});
我能够创建一个新 invoiceItem 并将其直接推送到 invoice invoiceItems 数组中,并在其中读取它,但我在尝试更新和删除时遇到了很多麻烦。我在看这个网站
http://tech-blog.maddyzone.com/node/add-update-delete-object-array-schema-mongoosemongodb
但我似乎无法让这些工作。
我在那篇博客中知道他正在使用 $set 来更新内容,但看起来它只更新一个字段,我想根据用户输入而不是硬编码字段来更新任何字段。我现在没有任何代码可以显示,因为我很迷茫,但我们将不胜感激!
【问题讨论】: