基本上,我有一个Topic架构。如果一个主题被删除,我想删除其他主题。想象一下删除节点意味着删除边缘的图表。
var schema = new Schema({
title: { type: String, required: true, trim: true },
srcId: { type: Schema.Types.ObjectId, validate: [edgeValidator, 'Set both srcId and destId, or neither'] },
destId: Schema.Types.ObjectId,
});
我希望第二个mongo删除在schema.pre('remove', ...)
但我现在还没有模特。所以调用.find()或.remove()不起作用。什么是最好的方式?
schema.pre('remove', function(next) {
var query = '';
var self = this;
if (this.isEdge) {
query = 'MATCH ()-[r:CONNECTION { mongoId: {_id} }]-() DELETE r;';
} else {
query = 'MATCH (n:Topic { mongoId: {_id} })-[r]-() DELETE n,r;';
}
// This is another database.
neo.query(query, this, function(err, data) {
if (err) return next(err);
if (self.isEdge) {
return next();
} else {
// Now we're back to mongoose and mongodb
// Find and remove edges from mongo
schema.find({ mongoId: { // <------ .find() is undefined
$or: [
{ srcId: self._id },
{ destId: self._id }
]
}}, function(err, edges) {
edges.remove(next);
});
}
});
});
答案 0 :(得分:1)
事实证明这很容易。
var Model = null;
var schema = ...
module.exports = Model = mongoose.model('Topic', schema);
然后只需在预删除中使用Model。一块馅饼。