Monthly Archives

October 2021

MongoDB | Importing data from CSV file

By | MongoDB Learning
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.
  • --drop: Specifies that we want to drop the collection before importing documents.
  • -u: specifys the user to log in

MongoDB | Securing Server

By | MongoDB Learning
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:

1) Switch to database you want to add user

use kunal

2) Then apply role

db.createUser(
{   
    user: "kunal",
    pwd: "kunal",
    roles:[
            {
                role: "readWrite", 
                db:"kunal"
            }
          ]
})

MongoDB | Geospatial Query Additional Notes

By | MongoDB Learning

$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.

MongoDB | Geospatial Query Part - III

By | MongoDB Learning

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