【发布时间】:2020-10-22 09:25:09
【问题描述】:
我正在使用 nodejs 和 mongoose 将数据保存在 mongodb 中。 我有这样的架构。我想保存一个将这些对象作为数组元素的数组
const gatePassSchema = new mongoose.Schema({
sno: {
type: String,
required: true
},
modeOfTransport: {
type: String,
required: true
},
description: {
type: String,
required: true
},
quantity: {
type: Number,
required: true
},
unit: {
type: String,
required: true
},
issuedTo: {
type: String,
required: true
},
dateOfReturn: {
type: Date,
required: true
},
from: {
type: String,
required: true
},
to: {
type: String,
required: true
},
reason: {
type: String,
required: true
},
remark: {
type: String,
required: true
},
incomingRef: {
type: String,
required: true
}
})
到达服务器的请求正文如下所示:
[ { sno: '1',
modeOfTransport: 'oijoi',
description: 'oiouiu',
quantity: 5,
unit: 'number',
issuedTo: 'giug',
dateOfReturn: '2020-07-12T18:30:00.000Z',
from: 'iuhiu',
to: 'hiuhi',
reason: 'hiu',
remark: 'ih',
incomingRef: 'iuhilk' },
{ sno: '2',
modeOfTransport: 'avvss',
description: 'uihiuhiu',
quantity: 6,
unit: 'packet',
issuedTo: 'giukh',
dateOfReturn: '2020-07-05T18:30:00.000Z',
from: 'iuhi',
to: 'uihnvn',
reason: 'lop',
remark: 'ytfyvh',
incomingRef: 'psd' } ]
我希望它保存这种数据格式: [{gatePassSchema},{gatePassSchema}....] 我在网上找到的大多数实现都展示了如何将模型中的元素转换为数组,但是如果我想将整个模型用作数组呢?
【问题讨论】: