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})

Last updated