我正在进行房间预订项目。本质上有用户和预订。用户可以预订一次,预订也可由一位用户拥有。
当我删除预订时,我还需要更新用户对象中的引用。
apiRouter.route('/bookings/:id')
.delete(function(req, res){
Booking.remove({_id: req.params.id}, function(err, user) {
if (err) res.send(err);
// Do stuff ... then return a message
res.json({ message: 'The booking was thrown towards a singularity.'});
});
}
);
正如您所看到的,我希望回调能够根据我的架构向我提供与预订对象一起存储的用户对象:
var BookingSchema = new Schema({
user: {type : mongoose.Schema.ObjectId, ref : 'User' , required: true},
room: {type : mongoose.Schema.ObjectId, ref : 'Room' , required: true},
equipment: [{type : mongoose.Schema.ObjectId, ref : 'Equipment' }],
startDate: {type: Date, required: true},
endDate: {type: Date, required: true}
});
根据mongodb文档,版本2.6+返回WriteResult
成功结果
remove()
返回包含操作状态的WriteResult
对象。成功后,WriteResult
对象包含有关已删除文档数的信息:
WriteResult({ "nRemoved" : 4 })
http://docs.mongodb.org/manual/reference/method/db.collection.remove/
现在,在我的回调中的user
变量中,我通过调用.remove()
来获取已删除的文档数。无论如何,我是否只为了删除booking
对象而重写此行为 ?否则,我可以在删除发生之前将user._id
保存到作用域,并执行另一个查询以获取用户对象。只是好奇这是否可行。谢谢!