我错过了什么吗?与传统数据库相比,为什么ORM需要更多努力来同步关系。以下面为例。Mongoose和MongoDB,与关系不同步
var UserSchema = new Schema({
username: {type: String, required: true, index:{unique:true}},
password: {type: String, required: true},
events: [{type: Schema.Types.ObjectId, ref:'Event'}]
});
var EventSchema = new Schema({
_creator: {type:Schema.Types.ObjectId, ref: 'User'},
post: {type: String, required: true}
});
UserSchema有很多事件,其中事件模式属于一个用户又名创建者。
.delete(function(req, res){
Event.remove({
_id:req.params.event_id
},function(err,response){
if(err)
res.send(err);
res.json({message:'Successfully Deleted'});
});
});
如果我创建了一个事件,并添加用户对象,创建者,它成功地加入了表,但如果我删除的事件。该事件不再显示,但ID仍显示在用户查询下。
下面是我的用户查询的样子后,我删除属于用户
[
{
"_id": "5831be814f8df40646fed954",
"password": "$2a$10$MNbwpj0qi9.rhEPmCHe5mOc8crrEo/CwbxVSbjjLSw28GdcQ0O6Um",
"username": "klaus",
"__v": 1,
"events": [
"5831c216a3d6810692bdf381"
]
}
]
我怎么会去从用户模式移除ID事件?不将事件模式与用户模式链接起来会更好吗?为什么这是屁股疼痛,为什么这是未来?
我更新[我的回答(http://stackoverflow.com/questions/40723019/mongoose-and-mongodb-out-of-sync-with-relationships/40723300#40723300)。我希望现在更有帮助。 – rsp