【问题标题】:What is the correct syntax for key value pair in mongoose?猫鼬中键值对的正确语法是什么?
【发布时间】:2017-10-18 09:09:46
【问题描述】:
var schema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    password: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    polls: [{type: Schema.Types.ObjectId, ref: 'Poll'}]
    /*This is incorrect*/votes: [{{type: Schema.Types.ObjectId, ref: 'Poll'},{type: number}}]
});

【问题讨论】:

  • 如果网站提到您的问题可以使用更多正文,那么请阅读How to Ask,了解如何在该正文中添加有意义的内容,而不是滥用标记并将您的标题简单地复制到您的问题中。
  • @CodeCaster 好吧,我不知道还有什么要说的:/ 这只是一个语法问题。

标签: mongodb mongoose


【解决方案1】:

要为您的 votes 字段提供有意义的结构,请使用对象数组而不是值对(在 Mongoose 中无法严格定义):

votes: [{
    poll: {type: Schema.Types.ObjectId, ref: 'Poll'},
    count: {type: number}
}]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 2018-04-15
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多