【发布时间】:2012-06-08 17:47:23
【问题描述】:
在我的图像共享应用程序中,您可以创建相册并向其中添加图像。当一张图片从网站上删除时,它也应该从存储图片引用(名称、id)的相册中删除。
我需要帮助的是找出哪些相册存储了即将删除的图像(参考)。
到目前为止,我尝试过以下路线,但查询时出现错误。我检查了 Mongodb 文档,语法如下:
db.collection.find( { field : { $in : array } } );
在我的路由中,字段和数组交换了位置,这似乎不起作用。
非常感谢您的帮助。提前致谢!
我的模型如下所示:
var AlbumSchema = new Schema({
title : String,
imageName : [String], <-- array the contains of images names
imageId : [String] <-- array the contains of images id's
});
modelObject.AlbumSchema = AlbumSchema;
modelObject.Album = mongoose.model('Album', AlbumSchema);
var ImageSchema = new Schema({
name : String,
size : Number,
type : String
});
modelObject.ImgSchema = ImgSchema;
modelObject.Image = mongoose.model('Image', ImgSchema);
删除图片的路径:
app.get('/blog/delete/:id', function(req, res){
model.ImagePost.findById(req.params.id, function (err, blog){
var theImage = blog.name;
if (err) {
console.log(err);
// do something
}
var query = albumModel.Album.find( { imageName: { $in : theImage } } );
query.exec(function (err, albums) {
if (!albums) {
console.log(err);
// do something
blog.remove(function(err) {
console.log(err);
// do something
});
res.redirect('/blogs');
}
else {
// code for removing the image(s) in the albums
res.redirect('/blogs');
}
});
});
});
【问题讨论】:
标签: arrays node.js mongodb mongoose