我想要的是根据Edit
的所有者/ Edit
找到Editor
并且Draft
所属的var DraftSchema = new Schema({
edits: [{type: Schema.ObjectId, ref: 'edits'}],
id: String,
publication_date: Date,
close_date: Date,
content: {type: Schema.ObjectId, ref: 'contents'}
});
var EditSchema = new Schema({
editor: {type: Schema.ObjectId, ref: 'users'},
start_date: Date,
last_edit: Date,
content: {type: Schema.ObjectId, ref: 'contents'}
});
var UserSchema = new Schema ({
id: String,
email: String,
salt: String,
hash: String,
drafts: [{type: Schema.ObjectId, ref: 'drafts'}]
});
(属于含义的参考)那个编辑)
这些是我的架构:
id
对于所有带pre
字段的Schema,有以下ObjectId
函数可以提供URL友好ID(我知道我覆盖了默认字符串UserSchema.pre('save', function(next){
if(this.id === undefined || this.id === null) {
this.id = makeid(6);
}
console.log("Here");
next();
});
:
EditSchema.statics.get_edit = function (draft_id, user_id, error, success) {
Draft.findOne({id: draft_id}).elemMatch('edits', {'editor.id': user_id})
.populate('edits.user').exec(function(err, draft) {
if (err || draft === null || draft === undefined) {return error(404);}
for (var i = 0; i < draft.edits.length; i++) {
console.log("IN LOOP");
if(draft.edits[i].user.id == user_id) {
return success(edits[i]);
}
}
error(404);
});
};
要找到正确的编辑,我有以下函数(draft_id和user_id引用上面函数中定义的id):
ObjectId
注意:只是想一想,过滤是否允许您过滤.findOne({"owner.owner_id"})
。与owner
中的{{1}}是ObjectId一样?如果没有,你会怎么做到这一点?我认为这是问题的一部分。