Website Update

Finally I’ve decided to leave the WordPress platform and move to static website generator. For this purpose I’m using Hugo platform, which is really great. Exporting from WordPress was easy, just needed few checks and it was done with breeze. I’ve deleted few post which were having Macromedia flash content or links which were broken. Hope for the best!

August 5, 2023 · Kunal Gautam

MongoDB | Importing data from CSV file

mongoimport -h lan.ikunal.in --type csv -d hospitals -c list --headerline --drop hospital_directory.csv -u hospitals –type: The input format to import: json, csv, or tsv. We are using csv so that’s what we specify. -d: Specifies what database to use. We used the hospitals database. -c: Specifies what collection to use. We used a collection called list. --headerline: Specifies that the first row in our csv file should be the field names....

October 7, 2021 · Kunal Gautam

MongoDB | Securing Server

use admin db.createUser({user:"adminUser",pwd:"adminPassword", roles:["userAdminAnyDatabase"]}) or db.createUser({ user:'adminUser', pwd:'adminPassword', passwordDigestor:'server', roles:['dbOwner'] }) Add the following to /etc/mongod.conf or /usr/local/etc/mongod.conf security: authorization: enabled Restart mongodb server To apply user with read write access to a database: Switch to database you want to add user use kunal Then apply role db.createUser( { user: "kunal", pwd: "kunal", roles:[ { role: "readWrite", db:"kunal" }] })

October 6, 2021 · Kunal Gautam

MongoDB | Geospatial Query Additional Notes

$near returns “nearest” as the naming suggests. And funny enough $geoWithin means “within the specified boundaries”. In short, $near “sorts” the results to return, and that is always going to take more time than not sorting. So it depends on what you want. If “order” of “nearest” is important, then you use $near. If it is not, then use $geoWithin and a plain definition of a circle. Which is the only polygon the two share in common....

October 6, 2021 · Kunal Gautam

MongoDB | Geospatial Query Part - III

Find within Radius. db.places .find({ location: { $geoWithin: { $centerSphere: [[-122.462621, 37.770100], (1 / 6378.1)] } } }) .pretty() It takes two arguments, first cordinates and second radians for distance value. For 1 Km radian value is 1 / 6378.1 for 2 Km it is 2 / 6378.1. For 1 mile it is 1 / 3963.2

October 5, 2021 · Kunal Gautam

MongoDB | Geospatial Query Part - II

Let say we need to find if user is within polygon area. Setting points of polygon const p1 = [-122.45476, 37.77488] const p2 = [-122.453, 37.76637] const p3 = [-122.5104, 37.76397] const p4 = [-122.51115, 37.77134] db.areas.insertOne({ name: 'Golden Gate Park', area: { type: 'Polygon', coordinates: [[p1, p2, p3, p4, p1]] } }) Set Index. db.areas.createIndex({area: "2dsphere"}) Find if Point inside polygon area db.areas .find({ area: { $geoIntersects: { $geometry: { type: 'Point', coordinates: [-122....

October 4, 2021 · Kunal Gautam

MongoDB | Geospatial Query Part - I

First we need to set index as 2dsphere db.places.createIndex({location: "2dsphere"}) Then provide the input to find nearby. Distance is in Meters. db.places .find({ location: { $near: { $geometry: { type: 'Point', coordinates: [-122.4626966, 37.7694006] }, $maxDistance: 130, $minDistance: 10 } } }) .pretty(); To Find withing Polygon area Setting points of polygon const p1 = [-122.45476, 37.77488] const p2 = [-122.453, 37.76637] const p3 = [-122.5104, 37.76397] const p4 = [-122....

October 3, 2021 · Kunal Gautam

MongoDB | GeoJSON Object

GeoJSON Objects Overview MongoDB supports the GeoJSON object types listed on this page. To specify GeoJSON data, use an embedded document with: a field named type that specifies the GeoJSON object type and a field named coordinates that specifies the object’s coordinates. If specifying latitude and longitude coordinates, list the longitude first and then latitude: Valid longitude values are between -180 and 180, both inclusive. Valid latitude values are between -90 and 90, both inclusive....

October 2, 2021 · Kunal Gautam

MongoDB | Geospatial Data

Insert data db.places.insertOne({ name: 'California Academy of Sciences', location: { type: 'Point', coordinates: [-122.4640133, 37.7690971] } }) Type is GeoJSON Object. Cordinates need to be provided first value as Longitude and second value as latitude

October 1, 2021 · Kunal Gautam

MongoDB | Index

To get index lists db.tableName.getIndexes() To set Index db.contact.createIndex({age:1}) It will create index in asc order db.contact.createIndex({age:-1}) It will create index in desc order db.contact.createIndex({age:1, gender:1}) Create group of index with age and gender both done in asc order. To Delete Index db.contact.dropIndex({"age": 1}) Value of key need to be 1 or -1 based on how index was created. If asc, 1 and for desc it is -1 Create Unique index...

September 30, 2021 · Kunal Gautam