在我的图像共享应用程序中,您可以创建相册并向其中添加图像。当一张图片从网站上删除时,它也应该从存储图片引用(名称、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');
}
});
});
});
最佳答案
db.collection.find( { field : { $in : array } } );
不是您想要的语法。这就是说“给我找到这个字段包含我要在数组中给你的列表值之一的文档。”
你想在 reaching into the array 时使用相等.
db.collection.find( { imageName:theImage } )
这表示找到 imageName 为 theImage 的文档,因此这将返回在其 imageName 数组中包含此图像的相册(或相册,如果一张图片可以在多个相册中)。
关于arrays - 在数组字段中查找匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10884948/