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

时间:2016-09-15 05:11:17

标签: 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属性循环遍历我的数组,并检查每个对象(如果它存在于数据库中并返回数据)?

1 个答案:

答案 0 :(得分:1)

无需迭代数组,将 $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 });

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

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