MongoDB

IN PRACTISE - basic

Sebastian Sztyper

Lead frontend developer

@ssztyper

Agenda

MongoDB configuration locally
Application of nonrelational bases
Comparison of relational and non-relational bases
Presentation of mongoDB databases in the Blachotrapez project
Creating backups
Import into the local environment
Inquiries
Aggregations
Bonus - methods of implementation in projects (if there is enough time)

is a cross-platform document-oriented database program.
Classified as a NoSQL database program,
MongoDB uses JSON-like documents
with optional schemas.

Mongo file structure

Compare relational
vs non-relational database

Data modeling
referencing and embeding

{
   _id: <ObjectId1>,
   username: "123xyz"
}
{
   _id: <ObjectId2>,
   user_id: <ObjectId1>,
   phone: "123-456-7890",
   email: "xyz@kotrak.com"
}
{
   _id: <ObjectId3>,
   user_id: <ObjectId1>,
   level: 5,
   group: "dev"
}
{
   _id: <ObjectId1>,
   username: "123xyz",
   contact: {
      phone: "123-456-7890",
      email: "xyz@kotrak.com"
   },
   access: {
      level: 5,
      group: "dev"
   }
}

vs

Some of the common applications
of NoSQL database

Social applications
Online advertisement/BI
Archiving Data

Big Analytic Workloads

Advantages

No specific pattern
Quick save
json :)
Low costs

Async driver - non blocking architecture friendly

Faster than classic relational bases

Disadvantages

Takes up a lot of space (in MongoDB 3.x this has been improved)
Deleting documents from the collection does not free disk space
Slow data aggregation

Less efficient with complex queries

Blachotrapez LogsStore

Blachotrapez TrapeziumDatabase

Hangfire

Backup

mongoexport --db DatabaseName -c collectionName savedFileName.json


Location of the exported file


Import - configure local db

Download and instal mongoDB from doc.mongodb.com

Create folder data/db in mongodb/bin

 


Import - ExceptionLogs

Download and instal mongoDB from doc.mongodb.com

Create folder data/db in mongodb/bin
Import file ExceptionLog.json

 

mongoimport --db TrapeziumDatabase -collection ExceptionLog --drop --file data/db/Exception.log


Start ExceptionLogs db

mongod --dbpath=data/db


Robo 3T / Robomongo


Query - find/sort

db.ExceptionLog.createIndex({Timestamp: -1})
db.getCollection('ExceptionLog').find().sort({Timestamp: -1})


Query - find/sort

db.getCollection('ExceptionLog').find().sort({Timestamp: -1})


Query - find range

db.getCollection('ExceptionLog').find({Timestamp: { $gte: ISODate("2020-06-10T13:53:42.992Z"), $lt: ISODate("2020-06-12T13:53:42.992Z")}}).sort({Timestamp: -1})


Query - find nested objects

db.getCollection('ExceptionLog').find({ "Details.Message": "Concurrency Exception" })


Query - find specyfic char

db.getCollection('ExceptionLog').find({ "Details.InnerException.Source": /Core .Net SqlClient Data Provider/})


Query - find specyfic char Hangfire

db.getCollection('Hangfire.job').find( 
{ 
    'Arguments' : { 
    	'$regex' : "http://10.10.10.87:80", '$options' : 'i' 
    },
    'CreatedAt': { 
    	$gte: ISODate("2020-04-16 10:58:29.441Z"), $lt: ISODate("2020-06-16 13:31:52.069Z")
    }
})


Query - distinct

db.getCollection('ExceptionLog').distinct("UserName")


Agregations - sum/single field

db.getCollection('ExceptionLog').aggregate([
  {$group:
    {
      _id : "$LogType",
      num_logs : {$sum : 1}
    }
  }
])


Agregations - sum/multi fields

db.getCollection('ExceptionLog').aggregate([
  {$group:
    {
      _id: {
          UserName : "$UserName",
          LogType : "$LogType"
        },
      num_logs : {$sum : 1}
    }
  }
])


Agregations - sum/multi fields Hangfire

db.getCollection('Hangfire.job').aggregate([
  {$group:
    {
      _id: {
          StateName : "$StateName",

        },
      num_states : {$sum : 1}
    }
  }
])

Questions

MongoDB in practise - basic

By Sebastian Sztyper

MongoDB in practise - basic

  • 80