MongoDB Fundamentals: Part 1 - Getting to Know the Basics

·

3 min read

Today's article is about MongoDB basic. where I will cover CRUD operations

Let's first discuss what MongoDB is and why we need it.

MongoDB is a NoSQL database that stores data in a JSON-like format called BSON, which is a binary representation of JSON documents.

//eg of Nosql and going to use it to do all CRUD operations

{_id: ObjectId("5effaa5662679b5af2c58829"), // auto generated unique id for every document
 email: “email@example.com”,
 name: {given: “Jesse”, family: “Xiao”},
 age: 31,
 addresses: [{label: “home”,
              street: “101 Elm Street”,
              city: “Springfield”,
              state: “CA”,
              zip: “90000”,
              country: “US”},
             {label: “mom”,
              street: “555 Main Street”,
              city: “Jonestown”,
              province: “Ontario”,
              country: “CA”}]

}

For example, if you have a collection, it's similar to a table in SQL.

// To see all the dbs
show dbs
// output will be all your collections name

use collection name// for example you want to work on users collection/table
use users // by this you can work on users collection

//if you want to create a new collection 
db.createCollection("users", {
                    "capped":true,
                     "size":90000,
                     "max":true })

Let's explore how the "insert" method works:

// first have to select users collection
use users
// insert one documents inside users collection
// InsertOne
db.users.insertOne({
"email": "john.doe@gmail.com",
 "name": {
 "given": "John",
  "family": "Doe"
  },
  "age": 28})

// insert multiple documents inside users collection 
// Insert Many Documents
db.users.insertMany({
"email": "steve.Robert@gmail.com",
 "name": {
 "given": "steve",
  "family": "Robert"
  },
  "age": 24}
},
"email": "Peter.England@gmail.com",
 "name": {
 "given": "Peter",
  "family": "England"
  },
  "age": 29})

Let's explore how the "find" method works:

// to find all document present in users collection

db.users.find({})

// to find specific document present in users collection
// lets find all the doc whose age is 29

db.users.find({age:29})

// if we only want a specific field in a document 
// eg want only  given name and age in all collections
db.users.find({}, {
name:{
given:1}, // here 1 means allow this field and  0 for remove it from the result
age:1
})

Let's explore how the "update" method works:

// update the first document
// only update one document
db.users.updateOne({"email": "Peter.England@gmail.com"},
{
$set:{
age:30
}
}) // can use upsert method as true it will create collection if not present
 // update many example 

db.user.updateMany(
  { "age": { $gt: 25 } }, // Filter to match documents with age greater than 25
  { $set: { "age": 30 } } // Update to set age to 30
)

Finally, let's discuss the "delete" method:

// to delete one document present in users collections
db.users.deleteOne({ email: "Peter.England@gmail.com" })


// to delete more then one doc
db.users.deleteOne({"age":{$gt:25}})

There are a few comparison operators that you can use to filter, update, or delete a document

$eq-> find document where values are equal
db.users.find({ age: { $eq: 30 } })

$lt-> find document where age is  less the 30
db.users.find({ age: { $lt: 30 } })

$gt-> find doc which are greate then 30
db.users.find({ age: { $gt: 30 } })

$lte-> find doc which are less then or equal to age 24
db.users.find({ age: { $lte: 24 } })

In this guide, we explored how CURD works in the MongoDB database.

I hope you found this blog useful. If so, please consider sharing it with your friends and I would appreciate your feedback. You can also reach out to me on Twitter at @lalitaswal2, and I would be happy to hear your thoughts.