mongoose中键值对的正确语法是什么?

时间:2017-05-18 13:52:32

标签: mongodb mongoose

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}}]
});

1 个答案:

答案 0 :(得分:1)

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

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