【发布时间】:2019-09-15 05:40:04
【问题描述】:
我无法在 mongoose 模式中理解以下语法。
tokens:[{
token:{
type: 'String',
required: true
}
}]
通常,当我想将对象添加到数组时,我只会推送对象。这包括如果我想将具有内部对象的对象推送到所述数组,如下例所示。
let obj =[]
obj.push({name: 'dirk', age: 24})
obj.push({name: 'chris', age:29, clothes: {shirt: 'black', pants: 'jeans'}, height: '6ft'})
我对猫鼬感到困惑,为什么我需要这种语法 [{}] 来使用数组?
【问题讨论】:
-
提醒一下,我从 mongoose 的文档示例中得到了该语法 - var Empty4 = new Schema({ any: [{}] });
-
那么你的问题是为什么你会有
any: [{}]或者为什么你不能使用obj = []然后就开始推送? -
@MichaelPlatt 后者。为什么我可以使用 obj = [ ] 并开始推送
标签: javascript node.js mongoose