Henry Zhu
Henry Zhu

Reputation: 2618

Node.js: How to deal with looking up different cities with the same name in a dictionary?

Context: I am building a Node.js application that needs to keep track of the number of active users in cities.

On the server-side, there is a mapping of city names to the number of active users in the city. For example, "Las Vegas" : 5. However, for different cities with the same name, such as Newport, WA and Newport, RI, the server can't properly update the active user count because of a duplicate key.

Adding the state into the key doesn't work because that's a USA-specific solution (e.x. [city name]-[state]) and adding the country into the key doesn't work either (e.x. [city-name]-[country]) because that doesn't cover different cities with the same name in one country.

I've looked into embedding latitude and longitude data into the key but that's probably impossible to use because city boundaries vary so much. Thing is, I can ensure that different cities with the same name appear in the dictionary as different keys with some format, like city-[longNum]-[latNum]. Client-side, I can get the city name, longitude and latitude coordinates, but obviously, a client's coordinates aren't going to be exactly the same as the center of the city's coordinates so there is no way of locating a client's city correctly in the server dictionary using the format city-[longNum]-[latNum].

Is there any way to locate the client's correct city from the dictionary/mapping if the client is in a duplicate city? I was thinking about using Redis' geospatial indexing but I'm not sure if that can apply to this problem.

Upvotes: 2

Views: 471

Answers (1)

Yola
Yola

Reputation: 19043

This is rather question about how to design a policy for city storing city names not about working with dictionary.

You can place information of a city in a structure like the following and serialize it.

{
  "country": "USA",
  "name": "Newport",
  "subdivision": "WA",
}

You can use more fields, some of which could be empty depending on other fields. Like for some countries you don't store subdivisions.

As another option you can redesign your client side to use query information using some kind of unique identifiers for cities. And query their textual names from the server as well. But that's depend on how data is represented in your map.

Upvotes: 1

Related Questions