- This code implementation is written in Java programming language.
- This is the project for the MCDP certification and is based on the tasks (assignment) given for it.
- Below are the installation instructions, tested use cases, scripts.
- Clone the repository by running the
git clonecommand. - Java jdk to be installed and setup on your machine.
- Maven (mvn) should be installed and setup on your machine
mvn compilemvn packagejava -jar <JAR_NAME> <MONGODB_CONNECTION_STRING>- By default application runs on MongoDB
localhost:27017 - Application runs on port
localhost:5001. Change the port in application as per requirement.
- This application is successfully tested with multiple scenarios
- 2102 cities and 200 planes
- 5000 cities and 400 planes
- 141 cities and 20 planes
- Initially load all the cities given with worldcities.csv.
mongoimport --uri mongodb://localhost:27017 --headerline -d logistics -c worldcities --type=csv worldcities.csv
mongo --host localhost --port 27017
show dbs
use logistics
minsize = {$match:{population:{$gt:500000}}}
sortbysize = { $sort : { population: -1 }}
onepercountry = { $group : { _id: "$country", city : { $first : "$$ROOT" }}}
format = { $project : { _id: "$city.city_ascii", name: "$city.city_ascii", location:["$city.lng","$city.lat"] , country: "$city.country" }}
newcollection = { $out : "cities" }
db.worldcities.aggregate([minsize,sortbysize,onepercountry,format,newcollection])
firstN = { $sample: { size: 20} }
addidone = { $group: { _id: null, planes : { $push : { currentLocation :"$location", location :"$_id" }}}}
unwind = { $unwind : {path: "$planes", includeArrayIndex: "id" }}
format = {$project : { _id : {$concat : ["CARGO",{$toString:"$id"}]}, callsign : {$concat : ["CARGO",{$toString:"$id"}]},currentLocation: "$planes.currentLocation", landed:"$planes.location", heading:{$literal:0.0}, route: []}}
asplanes = { $out: "planes"}
db.cities.aggregate([firstN,addidone,unwind,format,asplanes])
db.cities.createIndex( { "location" : "2dsphere" } )
db.cargos.createIndex( { "location" : 1, "status":1 } )
To run application on top 15 cities of countries with population greater than 1000.
- This script does the concatenation of city & country name and removal of '/' charater from the cities names.
- Concatenation of city and country name is required, because there are multiple cities with same name and have a unique index on city.
- Removal of '/' is required, because '/' character creates problem with API URI.
matchByMinPopulation = { $match: {population:{$gt:1000}}}
sortByPopulation = { $sort : { population: -1 }}
groupByCountry = { $group: { _id: "$country", city : { $push : '$$ROOT' }}}
projectForSlicing = { $project: {_id:1, "city": { "$slice": [ "$city", 15 ] }}}
unwindCity = { $unwind: "$city"}
addFieldsCityCharArray = { $addFields: { cityCharArray: {"$map": {input: { "$range": [ 0, { "$strLenCP": "$city.city_ascii" } ] },in: { "$substrCP": ["$city.city_ascii", "$$this", 1 ] }}}}}
addFieldsFilteredCityCharArray = { $addFields: { filteredCityCharArray: { "$filter": {input: "$cityCharArray", cond: { "$regexMatch": { input: "$$this", regex: "^[^/]+$" } }}}}}
addFieldsFilteredCityString = { $addFields: { filteredCityString: {"$reduce": { input: "$filteredCityCharArray", initialValue: "", in: { "$concat": [ "$$value", "$$this" ] }}}}}
projectCity = { $project : { _id: {$concat : ["$filteredCityString", "-", "$city.country"]}, name: {$concat : ["$filteredCityString", "-", "$city.country"]}, location:["$city.lng","$city.lat"] , country: "$city.country" }}
outCities = { $out : "cities" }
db.worldcities.aggregate([matchByMinPopulation,sortByPopulation,groupByCountry,projectForSlicing,unwindCity,addFieldsCityCharArray,addFieldsFilteredCityCharArray,addFieldsFilteredCityString,projectCity,outCities])
This is a modified script, to create 200 planes.
firstN = { $sample: { size: 200} }
addidone = { $group: { _id: null, planes : { $push : { currentLocation :"$location", location :"$_id" }}}}
unwind = { $unwind : {path: "$planes", includeArrayIndex: "id" }}
format = {$project : { _id : {$concat : ["CARGO",{$toString:"$id"}]}, callsign : {$concat : ["CARGO",{$toString:"$id"}]},currentLocation: "$planes.currentLocation", landed:"$planes.location", heading:{$literal:0}, route: []}}
asplanes = { $out: "planes"}
db.cities.aggregate([firstN,addidone,unwind,format,asplanes])
db.cities.createIndex( { "location" : "2dsphere" } )
db.cargos.createIndex( { "location" : 1, "status":1 } )