Bri*_*rig 12 mongoose mongodb node.js
我想在活动日志的详细信息中有一个嵌套对象.查看示例.如何在mongoose中定义模式?
activity: {
date: '1/1/2012' ,
user: 'benbittly',
action: 'newIdea',
detail: {
'title': 'how to nest'
, 'url': '/path/to/idea'
}
activity: {
date: '1/2/2012' ,
user: 'susyq',
action: 'editProfile',
detail: {
'displayName': 'Susan Q'
, 'profileImageSize': '32'
, 'profileImage': '/path/to/image'
}
Run Code Online (Sandbox Code Playgroud)
Kyl*_*ker 13
使用" 混合"类型,它允许您在示例中存储任意子对象.
var Activity = new Schema({
date : Date
, user : String
, action : String
, detail : Mixed
})
Run Code Online (Sandbox Code Playgroud)
要在模式中指示任意对象(即"任何东西"),您可以使用该Mixed类型或简单地使用{}.
var activity: new Schema({
date: Date,
user: String,
action: String,
detail: Schema.Types.Mixed,
meta: {} // equivalent to Schema.Types.Mixed
});
Run Code Online (Sandbox Code Playgroud)
然而,为了增加灵活性,有一个问题.使用Mixed(或{})时,您需要明确告诉mongoose您已经进行了如下更改:
activity.detail.title = "title";
activity.markModified('detail');
activity.save();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5321 次 |
| 最近记录: |