javascript - 如何循环遍历 Node/Express 中的对象数组并检查 MongoDB 数据库中是否存在匹配项?

标签 javascript node.js mongodb express

我正在使用 MEAN 堆栈和 Yelp API 构建一个 Web 应用程序,该应用程序返回一个对象数组,其中每个对象都是本地企业。我在前端处理这些数据,但在发送响应之前,我想检查 MongoDB 数据库中是否存在特定对象,但我正在努力解决如何做到这一点。

这是从 API 返回的对象:

[
    {
        "name": "Arendsnest",
        "url": "https://www.yelp.com/biz/arendsnest-amsterdam-2?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q",
        "snippet_text": "The reigning Lord of Amsterdam beer bars. Popular and seats go fast...come early. Ask for the massive all-Dutch beer list and prepare to have your...",
        "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/FurcfTuqaYBv_q34bGTK5g/ms.jpg"
    },
    {
        "name": "Bar Oldenhof",
        "url": "https://www.yelp.com/biz/bar-oldenhof-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q",
        "snippet_text": "So I'm not much of a drinker. My taste is highly selective and I usually prefer not to drink alcohol altogether. But my husband is the opposite so on a...",
        "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/1k57z7ziIW8MyAWHlXWGdg/ms.jpg"
    },
    {
        "name": "Beer Temple",
        "url": "https://www.yelp.com/biz/beer-temple-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q",
        "snippet_text": "This is a great place to stop in and have some American craft beer. With 30+ taps and a seemingly never ending list of bottle selections, you have many...",
        "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/yxUiYre1Y6ULqMhQ30NPOA/ms.jpg"
    },
    {
        "name": "Tales & Spirits",
        "url": "https://www.yelp.com/biz/tales-en-spirits-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q",
        "snippet_text": "This is exactly what every high-end cocktail bar should strive to have and be.\n\nFriendly staff: From the bartenders to the manager to the waitress. Everyone...",
        "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/IElXytpbY0bpp7ZdjFdGvA/ms.jpg"
    }
]

这存在于 MongoDB 数据库中:

{
    "_id": {
        "$oid": "57da26d8dcba0f51172f47b1"
    },
    "name": "Arendsnest",
    "url": "https://www.yelp.com/biz/arendsnest-amsterdam-2?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q",
    "snippet_text": "The reigning Lord of Amsterdam beer bars. Popular and seats go fast...come early. Ask for the massive all-Dutch beer list and prepare to have your...",
    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/FurcfTuqaYBv_q34bGTK5g/ms.jpg"
}

如何在 Node 中编写查询以使用 name 属性循环遍历数组,并检查每个对象是否存在于数据库中并返回数据?

最佳答案

无需迭代数组,使用 $or 运算符与包含您要查询的字段的映射数组。

以下面的示例为例,您要搜索两个属性的匹配项:

var yelp = [
    {
        "name": "Arendsnest",
        "url": "url1",
        "snippet_text": "foo",
        "image_url": "bar.jpg"
    },
    {
        "name": "Bar Oldenhof",
        "url": "abc",
        "snippet_text": "efg",
        "image_url": "ms.jpg"
    },
    {
        "name": "Beer Temple",
        "url": "https://www.yelp.com/",
        "snippet_text": "test",
        "image_url": "ms.jpg"
    },
    {
        "name": "Tales & Spirits",
        "url": "https://www.yelp.com/",
        "snippet_text": "This is exactly...",
        "image_url": "ms.jpg"
    }
],
query = yelp.map(function(item){ return { name: item.name, url: item.url }; });

db.collection.find({ "$or": query });

这将创建一个数组,您可以将其用作 $or find() 方法中的 表达式,相当于:

db.collection.find({
    "$or": [
        {
            "name": "Arendsnest",
            "url": "url1"
        },
        {
            "name": "Bar Oldenhof",
            "url": "abc"
        },
        {
            "name": "Beer Temple",
            "url": "https://www.yelp.com/"
        },
        {
            "name": "Tales & Spirits",
            "url": "https://www.yelp.com/"
        }
    ]
})
<小时/>

对于查询单个属性,例如您只想查询名称字段,最好使用 $in 运算符对此进行了更好的优化:

query = yelp.map(function(item){ return item.name; });
db.collection.find({ "name": { "$in": query } });

关于javascript - 如何循环遍历 Node/Express 中的对象数组并检查 MongoDB 数据库中是否存在匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39503533/

相关文章:

Javascript 无法将日期格式设置为工作日后跟日期

node.js - GruntJS + contrib-咖啡 : compiling but keeping the folder structure

android - 我可以在 Android 上安装 Node.js 吗?

mongodb - 服务器选择在 10000 毫秒后超时 - 无法将 Compass 连接到本地主机上的 mongoDB

mongodb - Mongoose 中groupBy后的总和

java - 使用java将大型BasicDBObject存储到mongodb时出现异常

javascript - 在 ember 插件中管理第三方依赖项的正确方法是什么?

javascript - 带延迟的工具提示在 ie 上不起作用

javascript - 在将响应发送回客户端之前,如何等待 Mandrill 的交付响应?

javascript - 如何获得处理 AJAX 函数以返回 bool 值的 PHP 函数?