如何根据数组中的数据过滤我的集合?

时间:2018-04-01 17:52:55

标签: node.js express mongoose

我有一个收藏品名称。

像这样的集合的文档

{
    "_id" : ObjectId("5ac0b89d08e21c226cc2992c"),
    "name" : "Lenovo Ideapad Yoga 510 (80S9002QIH) Laptop (AMD Dual Core A9/ 4GB/ 1TB/ Win10 Home)",
    "brand" : "Lenovo",
    "tags" : [
            "lenovo",
            "ideapad",
            "yoga",
            "510",
            "80s9002qih",
            "laptop",
            "amd",
            "dual",
            "p1301ktgc5jy"
    ],
    "specifications" : {
            "full_specs" : {
                    "General" : {
                            "Model" : "80S9002QIH",
                            "Utility" : "Everyday Use",
                            "Device Type" : "Hybrid",
                            "OS" : "Windows 10 Home (64-bit)",
                            "Warranty" : "1 Year Onsite Warranty"
                    },
                    "Display" : {
                            "Type" : "Full HD LED Backlit Display",
                            "Touch" : "Yes",
                            "Size" : "14 inches",
                            "Resolution" : "1920 x 1080 pixels",
                            "PPI" : "~ 157",
                            "Aspect Ratio" : "16"
                    },
                    "Connectivity" : {
                            "Ethernet" : "Gigabit Ethernet",
                            "WiFi" : "IEEE 802.11ac",
                            "Bluetooth" : "v4.0",
                            "Lan Port" : "Yes",
                            "USB Ports" : "1 x USB 2.0, 2 x USB 3.0",
                            "HDMI" : "1 x HDMI Port",
                            "Card Reader" : "4-in-1 Card Reader (SD, SDHC, SDXC, MMC)",
                            "Microphone In" : "Yes"
                    },
                    "Input" : {
                            "Camera" : "HD Webcam",
                            "Keyboard" : "Standard Keyboard",
                            "Pointer Device" : "Touchpad",
                            "Inbuilt Microphone" : "Built-in Microphone",
                            "Speakers" : ", Stereo Speakers",
                            "Optical Drive" : "No"
                    },
                    "Processor" : {
                            "Processor" : "AMD APU Dual Core A9 6th Gen A9-9410",
                            "Speed" : "2.9 GHz, Dual Core (Turbo Boost Upto 3.42 GHz)",
                            "Cache" : "2 MB",
                            "Brand" : "AMD",
                            "Series" : "APU Dual Core",
                            "Model" : "A9-9410"
                    },
                    "Graphics" : {
                            "GPU" : "Integrated AMD Graphics",
                            "Brand" : "AMD"
                    },
                    "Memory" : {
                            "RAM" : "4 GB DDR4",
                            "Hard Disk Capacity" : "1 TB",
                            "Hard Disk Speed" : "5400 RPM"
                    },
                    "Extra" : {
                            "Sales Package" : "2 in 1 Laptop, Battery, Power Adaptor, User Guide, Warranty Documents"
                    }
            }
    }
}

我有像

这样的数组
`var brand_arr = [ 'Acer', 'Asus', 'Dell', 'lenovo' ];
var cpuSpeed = [ '1.5', '2.0', '2.5', '3.0' ];`

我想对我的集合应用过滤,该集合检索我的集合中包含brand_arr数组中任何数据值的所有文档,然后匹配cpuSpeed数组的任何值。

brand_arr的数据包含文档品牌键的值,而cpuSpeed数组中的数据包含处理器下的速度值。

2 个答案:

答案 0 :(得分:1)

使用$in

product.find({brand: {$in: brand_arr}})

答案 1 :(得分:0)

如果将内存数组中的文档分配给documents等变量,则可以执行以下操作:

const filteredByBrand = documents.filter((document) => brand_arr.includes(document))

请注意,如果您的brand_arr包含的字符串与文档中的字符串不同,则需要考虑这一点。