This post was published at 28 Sep 2017


No relational database with data storage in JSON format.

  • Install MongoDB, first download keys:

    sudo -i
    sudo apt-key adv --keyserver hkp:// --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
    echo "deb xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
    sudo apt-get update
  • Then, install mongo

    sudo apt-get install -y mongodb-org
  • Create the mongo service file:

    sudo nano /etc/systemd/system/mongodb.service

    Add these lines:

      Description=High-performance, schema-free document-oriented database
      ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
  • Next, start the service:

    sudo systemctl start mongodb
    sudo systemctl status mongodb
    sudo systemctl enable mongodb
  • Finally, start the client:

    sudo mongod -f /etc/mongod.conf

Starts client

  • When you have an error in language, in the file .bashrc add:

    export LC_ALL=C

Manage the database

  • Create index:

    db.dbname.createIndex( { razon_social: "text", ruc: "text" } )
  • Get Indexes:

  • Delete index:



  • Import:

    mongoimport --db dbname --collection colname --file outputfile
  • Export:

    mongoexport --db dbname --collection colname --out  outputfile
  • Export to CSV format:

    mongoexport --db dbname --collection colname --query '{"id_moduleiot":"C001","date": {$gte:"2017-11-01"}}' --type csv --fields "field1,field2,field3" --out data.csv


  • Init the service on 27018:

    sudo mongod --auth --port 27018 --dbpath /var/lib/mongodb/
  • Create a master-daster user:

    use admin
      user: "hyper-admin",
      pwd: "hyper",
      roles: [ "root"]
  • Create a new user with admin privileges:

      user: "super-admin",
      pwd: "super",
      roles: [
        {role: "clusterAdmin", db: "admin" },
        {role: "dbAdminAnyDatabase", db: "admin" },
        {role: "userAdminAnyDatabase", db: "admin" },
        {role: "readWriteAnyDatabase", db: "admin" }
  • Create a new user with privileges only in mydb:

    use mydb
      user: "mydb-admin",
      pwd: "admin",
      roles: [
        {role: "userAdmin", db: "mydb" },
        {role: "readWrite", db: "mydb" }
  • Create a new user with onlye-read privileges on mydb:

      user: "mydb-user",
      pwd: "user",
      roles: [
        {role: "read", db: "mydb" }
  • Once created the service in --auth mode, we can connect to db using:

    mongo admin --port 27018 --username hyper-admin --password hyper --authenticationDatabase admin
    mongo admin --port 27018 --username super-admin --password super --authenticationDatabase admin
    mongo mydb --port 27018 --username mydb-admin --password admin --authenticationDatabase mydb
    mongo mydb --port 27018 --username mydb-user --password user --authenticationDatabase mydb