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

Insert Document

db.friendsCollection.insertOne(
{
    "firstname": "Monica",
    "lastname":"Geller",
    "age": 30,
    "location":"NYC",
    "profession":"chef"
}
)

  1. Command Purpose: db.friendsCollection.insertOne({...}) it is used to insert a single document into the friendsCollection. If friendsCollection doesn't exist, MongoDB will create it automatically when you insert the first document.

  2. Document Structure: The document being inserted is enclosed in curly braces {...}. It represents a single record or entry in the friendsCollection. This document is similar to a row in a relational database table but can have a complex, nested structure.

  3. Field-Value Pairs: Inside the document, data is stored as field-value pairs. For example, "firstname": "Monica" means there's a field named firstname with the value "Monica". Fields are similar to column names in a relational database, and values can be various data types (e.g., string, number, array, object).

  4. Data Types: MongoDB supports various data types. In this command, firstname, lastname, location, and profession are strings and age is a number.

  5. Collection: A collection is similar to a table in a relational database. It's a grouping of documents, usually with related information. In this case, friendsCollection might hold documents for each friend, including their name, age, location, and profession.

  6. Database: The db part refers to the database you're working with. Databases contain collections, and a MongoDB server can host multiple databases.

  7. Read and Write Operations: After inserting data, you can retrieve, update, or delete it using MongoDB's CRUD (Create, Read, Update, Delete) operations. For example, you could use db.friendsCollection.findOne({firstname: "Monica"}) to find Monica's document.

  8. Flexibility: MongoDB's schema-less nature means documents in the same collection don't need the same fields.

db.friendsCollection.find()

// "_id"

Autogenerated ObjectID consists of 12 bytes. It's of type BSON

4 bytes - Unix Epoch
3 bytes - machine identifier
2 bytes - process id
3 bytes - random value

Globally Unique: The first 9 bytes (timestamp, machine identifier, and process ID) indeed contribute to the global uniqueness of the ObjectId.

Automatic Indexing: By default, MongoDB automatically creates a unique index on the _id field for every collection, which helps in efficiently querying documents by their _id.

Hexadecimal Representation The ObjectId is displayed as 24 hexadecimal characters when represented as a string. This is because each byte (8 bits) of the ObjectId is represented as two hexadecimal characters (each hex digit represents 4 bits). The conversion to hexadecimal doubles the apparent length of the ObjectId when viewed as a string. BSON

Binary encoded JSON

Widely used to transmit and store data across web apps. JSON is human-readable.

BSON is encoded, making it easier for machines to read.

MongoDB stores data in BSON format both internally and over the network.

Advantages of BSON

  • Efficient

  • Rich Data Types

  • Field Indexing

How BSON is stored in the MongoDB Database

insertMany() is used to insert more than one document.

db.friendsCollection.insertMany([
{
    "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"
}
])
db.friendsCollection.insertMany([
{
    "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"
}
])

Index

MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.

db.friendsCollection.createIndex({"firstname" : 1} , {unique : true})
PreviousMongoDB CommandsNextQuerying MongoDB

Last updated 1 year ago

src:mongodb.com/basics/