w3resource

MongoDB Exercise - Get the names and addresses of restaurants with zipcodes starting with 10


Write a MongoDB query to find the name and address of the restaurants that have a zipcode that starts with '10'.

Structure of 'restaurants' collection :

{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}

Query:

db.restaurants.aggregate([
  {
    $unwind: "$address"
  },
  {
    $match: {
      "address.zipcode": /^10/
    }
  },
  {
    $project: {
name: 1,
      "address.street": 1,
      "address.zipcode": 1,
      _id: 0
    }
  }
])

Output:

{
address: { street: 'West   57 Street', zipcode: '10019' },
name: 'Dj Reynolds Pub And Restaurant'
  },
  {
address: { street: 'Victory Boulevard', zipcode: '10314' },
name: 'Kosher Island'
  },
  {
address: { street: 'East   66 Street', zipcode: '10065' },
name: '1 East 66Th Street Kitchen'
  },
  {
address: { street: 'Broadway', zipcode: '10003' },
name: "Bully'S Deli"
  },
  {
address: { street: 'East   74 Street', zipcode: '10021' },
name: 'Glorious Food'
  },
  {
address: { street: 'Southern Boulevard', zipcode: '10460' },
name: 'Wild Asia'
  },

.....

Explanation:

The said query in MongoDB that finds all restaurants in the restaurants collection that have an address with a zipcode starting with '10', and outputs the name of the restaurant as well as the street and zipcode fields of the matching address.

The $unwind creates a new document for each address element within the original document, effectively flattening the array.

The $match, filters the resulting documents where the zipcode field of the address sub-document matches the regex pattern /^10/. The ^ character specifies that the pattern should match the start of the field's string value.

The $project, which transforms the output of the query by excludes the _id field and includes only the name field and a new address sub-document field that contains only the street and zipcode fields of the original address sub-document.

The address.street and address.zipcode fields are mapped to the corresponding fields of the original address sub-document using the dollar sign ($) notation.

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Find out which restaurants have the word "coffee" in their names.
Next: Names and addresses of restaurants with a cuisine that starts with 'B'.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.