Mongoose - 如何向ObjectId引用数组添加额外的密钥?

时间:2016-09-29 17:22:06

标签: javascript node.js mongodb mongoose nosql

var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now },
    recipients: [{ type: Schema.ObjectId, ref: 'User' }],
    messages: [ conversationMessageSchema ]
});
mongoose.model('Conversation', conversationSchema);

在上面的示例中,您将如何在收件人数组中添加额外的属性,例如... status?

即。收件人:

[{ _id: { 
    type: Schema.ObjectId, 
    ref: 'User'
  }, 
  status { 
    type: String
  } 
}]

喜欢这个^?

由于

1 个答案:

答案 0 :(得分:-2)

你可以这样做

 var conversationSchema = new Schema({
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now },
    recipients: [
      name:{ type: Schema.Types.ObjectId,  ref: 'User' },
      status { type: String}
    ],
    messages: [ conversationMessageSchema ]
});
mongoose.model('Conversation', conversationSchema);