我正在使用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/

10-16 02:37
查看更多