To install

MongoDB configuration

Once you have installed MongoDB, you would need to configure the access, as well as where the data is located.

To do this, create a file locally, named mongod.cfg. This will include setting path to the data folder for MongoDB server, as well as to the MongoDB log file, initially without any authentication (last 2 lines being commented). Please update these local paths, with your local settings:

Run in command prompt next line. This will start the MongoDB server, pointing to the configuration file already created (in case the server is installed in a custom folder, please update first the command)

Once the server is started (and you could see the details in the log file), run mongo.exe in command prompt. The next step is to add the administrator user to the database. Run MongoDB with the full path (ex: “C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe”) and copy paste the next code in the console:

Stop the server, uncomment the last 2 lines from mongod.cfg file and then restart the MongoDb server.

MongoImport – Initialize the database with large datasets

We will start with Wikivoyage. The dataset was originally available here (link). To be easier to import it, I have slightly transformed it (changed to tab delimited file, and applied a minimum data cleaning). The file is available in Github (link).

The second dataset GeoNames is available in the same Github folder (link).

Running the script import.bat (found in the same folder as the datsets), will do the import of the data, creating also the a new database, called TravelDb and the associated indexes. Script is included here, but it would be better just to run the script file:

Fields files specifie the field names as well as their associated types. Using the option columnsHaveTypes we make the import with the types we need (e.g. int, double, string etc.).

The result should look like this:

MongoDB – LINQ support

The .NET Core solution included here follows the same structure as in my earlier article – Using MongoDB .NET Driver with .NET Core WebAPI . There, I have already presented a step by step guide on how to create an WebApi solution from scratch, connecting to MongoDB and implementing all the basic actions of a REST API.

In comparison, here, the web controller will implement just one action (GET) – focusing mainly just on running different queries:

How to find things to do in a specific city

Let’s assume we want to find the interesting things to do in a city. We either show all the items in the city, ordered by the type of action, or just select a specific action (e.g. buy, do, eat, drink etc.).

This method will be called by a GET function. Assuming that we want to search after interesting things to do in Paris (http://localhost:61612/api/travelquery/Paris?doAction=do) we get interesting results, and one of them is the next:

Running faster the queries

One way to improve the speed of the queries is to apply an index. Searching within the collection after City and Action would recommend to add a simple index with these two fields.

Executing the JavaScript file with mongo shell, will add an index on City, and then Action.

To continue

I would create a second part of this article, adding pagination support as well as aggregation enhancements brought by newer MongoDB versions, taking into consideration also the second dataset. Perhaps you knew these, maybe you learned a few things. Would you like to see something more covered ?