1

I am building a web app with the MEAN stack and Yelp API that returns an array of objects, where each object is a local business. I work with this data in the front-end, but before I send a response I want to check if a particular object exists in the MongoDB database and I am struggling with how to do that.

Here is an object that is returned from the 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"
    }
]

This exists in the MongoDB database:

{
    "_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"
}

How can I write a query in Node to loop through my array using name property and do a check on every object if it exists in the database and return the data?

4
  • You could use for each loop to get all names and push them into array and then use $in operator in the query db.collection.find('name': {$in: namesArray}); Commented Sep 15, 2016 at 5:29
  • that should work! what if I want to search for a match of 2 properties? for example name and url? Commented Sep 15, 2016 at 5:55
  • There are other operator like $or or $and. db.collection.find('$and': [{name:'....'},{url: '....'}]);I would recommend to check mongodb docs :) Commented Sep 15, 2016 at 5:59
  • Actually I'm not sure how to do this along with $in operator. The above only works with single name and url. Maybe the only way is to run multiple queries. Commented Sep 15, 2016 at 6:04

1 Answer 1

2

No need to iterate the array, use the $or operator with a mapped array that has the fields you want to query.

Take the following example where you want to search for a match of two properties:

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

This will create an array that you can use as the $or expression in your find() method, equivalent to :

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/"
        }
    ]
})

For querying on single properties, say for instance you want to query on just the name field, better use the $in operator which is better optimised for such:

query = yelp.map(function(item){ return item.name; });
db.collection.find({ "name": { "$in": query } });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.