node.js 在数组字段中查找匹配项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10884948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:49:56  来源:igfitidea点击:

Finding a match in an array field

arraysnode.jsmongodbmongoose

提问by holyredbeard

In my image sharing application you can create albums and add images to them. When an image is deleted from the site, it should as well be removed from the album(s) that stores references to the image (name, id).

在我的图像共享应用程序中,您可以创建相册并向其中添加图像。当从网站上删除图像时,它也应该从存储对图像的引用(名称、ID)的相册中删除。

The thing I need help with is to find which albums that has stored the image (reference) that's about to be removed.

我需要帮助的是找到哪些相册存储了即将删除的图像(参考)。

In the route below is what I've tried so far, but I get an error on the query. I've checked the Mongodb docs and the syntax looks like this:

在下面的路线中是我迄今为止尝试过的路线,但我在查询中遇到错误。我检查了 Mongodb 文档,语法如下所示:

db.collection.find( { field : { $in : array } } );

In my route the field and the array has switched places, which doesn't seem to work.

在我的路线中,字段和数组已经交换了位置,这似乎不起作用。

I would really appreciate some help. Thanks in advance!

我真的很感激一些帮助。提前致谢!

My models looks like the following:

我的模型如下所示:

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);

The route for deleting an image:

删除图片的路径:

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');
            }
        });
    });
});

回答by Asya Kamsky

db.collection.find( { field : { $in : array } } );is not the syntax that you want. That says "Find me the document where this field has one of the list values that I'm going to give you in an array."

db.collection.find( { field : { $in : array } } );不是您想要的语法。上面写着“找到我要在数组中提供的列表值之一的文档,其中该字段具有列表值之一。”

You want to use equality while reaching into the array.

您想在进入数组时使用相等性。

db.collection.find( { imageName:theImage } )

db.collection.find( { imageName:theImage } )

This says find me the document where imageName is theImage so this will return the Album (or Albums if a picture can be in more than one album) which contains this image in its imageName array.

这表示为我找到 imageName 是 theImage 的文档,因此这将返回相册(如果图片可以在多个相册中,则返回相册),该相册在其 imageName 数组中包含此图像。