Big Data & Tools with NoSQL
  • Big Data & Tools
  • ReadMe
  • Big Data Overview
    • Overview
    • Job Opportunities
    • What is Data?
    • How does it help?
    • Types of Data
    • The Big 4 V's
      • Variety
      • Volume
      • Velocity
      • Veracity
      • Other V's
    • Trending Technologies
    • Big Data Concerns
    • Big Data Challenges
    • Data Integration
    • Scaling
      • CAP Theorem
      • Optimistic concurrency
      • Eventual consistency
      • Concurrent vs. Parallel Programming
    • Big Data Tools
    • No SQL Databases
    • What does Big Data learning means?
  • Linux & Tools
    • Overview
    • Linux Commands - 01
    • Linux Commands - 02
    • AWK
    • CSVKIT
    • CSVSQL
    • CSVGREP
  • Data Format
    • Storage Formats
    • CSV/TSV/Parquet
    • Parquet Example
    • JSON
    • HTTP & REST API
      • Terms to Know
        • Statefulness
        • Statelessness
        • Monolithic Architecture
        • Microservices
        • Idempotency
    • REST API
    • Python
      • Setup
      • Decorator
      • Unit Testing
      • Flask Demo
      • Flask Demo - 01
      • Flask Demo - 02
      • Flask Demo - 03
      • Flask Demo - 04
      • Flask Demo - 06
    • API Testing
    • Flask Demo Testing
    • API Performance
    • API in Big Data World
  • NoSQL
    • Types of NoSQL Databases
    • Redis
      • Overview
      • Terms to know
      • Redis - (RDBMS) MySql
      • Redis Cache Demo
      • Use Cases
      • Data Structures
        • Strings
        • List
        • Set
        • Hash
        • Geospatial Index
        • Pub/Sub
        • Redis - Python
      • Redis JSON
      • Redis Search
      • Persistence
      • Databases
      • Timeseries
    • Neo4J
      • Introduction
      • Neo4J Terms
      • Software
      • Neo4J Components
      • Hello World
      • Examples
        • MySQL: Neo4J
        • Sample Transactions
        • Sample
        • Create Nodes
        • Update Nodes
        • Relation
        • Putting it all together
        • Commonly used Functions
        • Data Profiling
        • Queries
        • Python Scripts
      • More reading
    • MongoDB
      • Sample JSON
      • Introduction
      • Software
      • MongoDB Best Practices
      • MongoDB Commands
      • Insert Document
      • Querying MongoDB
      • Update & Remove
      • Import
      • Logical Operators
      • Data Types
      • Operators
      • Aggregation Pipeline
      • Further Reading
      • Fun Task
        • Sample
    • InfluxDB
      • Data Format
      • Scripts
  • Python
    • Python Classes
    • Serialization-Deserialization
  • Tools
    • JQ
    • DUCK DB
    • CICD Intro
    • CICD Tools
      • CI YAML
      • CD Yaml
    • Containers
      • VMs or Containers
      • What container does
      • Podman
      • Podman Examples
  • Cloud Everywhere
    • Overview
    • Types of Cloud Services
    • Challenges of Cloud Computing
    • High Availability
    • Azure Cloud
      • Services
      • Storages
      • Demo
    • Terraform
  • Data Engineering
    • Batch vs Streaming
    • Kafka
      • Introduction
      • Kafka Use Cases
      • Kafka Software
      • Python Scripts
      • Different types of Streaming
    • Quality & Governance
    • Medallion Architecture
    • Data Engineering Model
    • Data Mesh
  • Industry Trends
    • Roadmap - Data Engineer
    • Good Reads
      • IP & SUBNET
Powered by GitBook
On this page
  1. NoSQL
  2. MongoDB

Data Types

  • String: Refers to plain text

  • Number: Consists of all numeric fields

  • Boolean: Consists of True or False

  • Object: Other embedded JSON objects

  • Array: Collection of fields

  • Null: Special value to denote fields without any value

{audience_rating: 6}
{audience_rating: 7.6}

{"title": "A Swedish Love Story", released: "1970-04-24"}
{"title": "A Swedish Love Story", released: "24-04-1970"}
{"title": "A Swedish Love Story", released: "24th April 1970"}
{"title": "A Swedish Love Story", released: "Fri, 24 Apr 1970"}

Variables

var name="Rachel Green"
name

var plainNum = 1299

plainNum

// forces MongoDB to use 32 bit
var explicitInt = NumberInt("1299")

explicitInt

// long forces 64 bit

var explicitLong = NumberLong("777888222116643")

explicitLong

var explicitLong_double = NumberLong(444333222111242)

explicitLong_double

// forces 128 bit

var explicitDecimal = NumberDecimal("142.42")

explicitDecimal

var explicitDecimal_double = NumberDecimal(142.42)

explicitDecimal_double

//values are rounded off
var decDbl = NumberDecimal(5999999999.99999999)

decDbl

// boolean demo

var isActive=true

isActive

var isDeleted=false

isDeleted

// objects

var friend={
    "first_name":"Rachel",
    "last_name":"Green",
    "email":"rachel@friends.com"
}

friend

friend.first_name


var friends1=[
{
    "firstname": "Phoebe",
    "lastname":"Buffay",
    "age": 31,
    "profession":"Therapist"
},
{
    "firstname": "Ross", 
    "lastname":"Geller",
    "age": 31,
    "location": "NY",
    "profession":"Palentologist",
    "spouses":["Carol","Emily","Rachel"]
},
{
    "firstname": "Chandler",
    "lastname":"Bing",
    "age": 31,
    "location": "NY"
},
{
    "firstname": "Joey",
    "lastname":" Tribianni",
    "age": 32,
    "location": "NYC",
    "profession":"actor"
}
]


var friends2 = [
{
    "firstname": "rachel",
    "lastname" : "green",
    "age" : 30,
    "location": "NYC",
    "profession":"Fashion Designer"
},
{
    "name":{"firstname":"Ben",
            "lastname" : "Geller"}, 
    "age" : 6,
    "location": "NYC"
},
{
    "name":{"firstname":"Emma",
            "lastname" : "Geller"},
    "age" : 1,
    "location": "NYC"
}
]

Skip - Limit

db.friendsCollection.find().sort({"lastname":1,"firstname":1}).limit(2)

db.friendsCollection.find().sort({"lastname":1,"firstname":1}).skip(2).limit(2)

Sort

db.friendsCollection.drop()

db.friendsCollection.insertMany(friends1)
db.friendsCollection.insertMany(friends2)

db.friendsCollection.find().sort({"firstname":1})
db.friendsCollection.find().sort({"firstname":-1})

Projection

MongoDB projections specify which fields should be returned in the documents that match a query. All fields are returned by default, but you can include or exclude specific fields to control the amount of data MongoDB returns.

Projections can help improve performance by reducing network bandwidth and processing time, especially for documents with large amounts of data or when you only need a subset.

Return firstname and lastname only

This query tells MongoDB to return only the firstname and profession fields of all documents in the friends1 collection. If you want to exclude the _id field as well, you can do so by explicitly setting it to 0 in the projection:

db.friendsCollection.find({},{"firstname":1,"lastname":1})

db.friendsCollection.find({},{"_id":0,"firstname":1,"lastname":1})

// Exclude Location

db.friendsCollection.find({ firstname: "Joey" }, { location: 0 })


db.friendsCollection.find({}, { "name.firstname": 1, age: 1 })

Distinct Count

db.friendsCollection.distinct("lastname")

db.friendsCollection.distinct("lastname",{"email":{$regex:".*friends"}})

db.friendsCollection.count()
// without condition, this will check the metadata and return the count. 
// It will not be accurate all the time due to multi-node architecture 
// and the time taken to sync.

db.friendsCollection.count({"lastname":"Geller"})


// newer syntaxes always query need to be passed, it’s slower

db.friendsCollection.countDocuments({"lastname":"Geller"})

// newer syntaxes to get an overall idea using metadata, quick result

db.friendsCollection.estimatedDocumentCount({"lastname":"Geller"})

Array Slice

db.friendsCollection.find({"firstname":{$eq:"Ross"}}, {"spouses":{$slice:1}})

db.friendsCollection.find({"firstname":{$eq:"Ross"}}, {"spouses":{$slice:-1}})
PreviousLogical OperatorsNextOperators

Last updated 1 year ago