Skip to content

MongoDB Notes and Setup + REST-API using NodeJS and MongoDB

Notifications You must be signed in to change notification settings

Sumonta056/MongoDB-RestAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📚 MongoDB and Mongoose Tutorial and Resources

This document provides a reference for common MongoDB commands and operations.

📃 Basic Commands

  • cls - Clear the screen.
  • exit - Exit MongoDB shell.

📃 MongoDB Shell

  • mongosh - Start the MongoDB shell.

📃 Database Operations

  • use TestDB - Switch to the TestDB database. If it doesn't exist, it will be created.
  • db.dropDatabase() - Drop the current database.

📃 Collection Operations

  • db.createCollection("students") - Create a collection named students.

📃 Document Operations

  • db.students.insertOne({name: "John", age: 25, gpa:3.2}) - Insert one document into the students collection.
  • db.students.find() - Find all documents in the students collection.
  • db.students.insertMany([{name: "Jane", age: 22, gpa:3.5}, {name: "Doe", age: 23, gpa:3.8}]) - Insert multiple documents into the students collection.

📃 Data Types in MongoDB

  • Insert a document with various data types:
    db.students.insertOne({
        "name": "larry",
        "age": 32,
        "gpa": 3.8,
        "fullTime": false,
        "registerDate": "2023-01-02T00:00:00Z",
        "graduationDate": null,
        "courses": ["ENG", "Math", "Chem"],
        "address": {
            "street": "123 KalirBazar",
            "city": "Narayanganj",
            "zipCode": 1300
        }
    })

📃 Query Operations (Sorting, Limiting, and Projection)

  • db.students.find({name: "Jane"}) - Find documents where the name is "Jane".
  • db.students.find().sort({name: 1}) - Sort documents by name in ascending order.
  • db.students.find().sort({name: -1}) - Sort documents by name in descending order.
  • db.students.find().sort({name: 1, age: -1}) - Sort documents by name in ascending order and age in descending order.
  • db.students.find().limit(2) - Limit the result to 2 documents.
  • db.students.find().sort({gpa: -1}).limit(1) - Find the document with the highest GPA.
  • db.students.find({}, {name: true}) - Find documents and return only the name field.
  • db.students.find({}, {_id: false, name: true}) - Find documents and return only the name field without the _id field.
  • db.students.find({}, {_id: false, name: true, gpa: true}) - Find documents and return the name and gpa fields without the _id field.

📃 Update Operations

  • db.students.updateOne({name: "Jane"}, {$set: {gpa: 3.9}}) - Update the GPA of the first document where the name is "Jane".
  • db.students.updateMany({name: "Jane"}, {$set: {gpa: 3.9}}) - Update the GPA of all documents where the name is "Jane".
  • db.students.updateOne({name: "Jane"}, {$unset: {gpa: ""}}) - Remove the GPA field from the first document where the name is "Jane".

📃 Delete Operations

  • db.students.deleteOne({name: "Jane"}) - Delete the first document where the name is "Jane".

📃 Advance Query Operations

  • db.students.find({name: {$ne: "Mridul"}}) - Find all documents where the name is not "Mridul".
  • db.students.find({age: {$gt: 25}}) - Find all documents where the age is greater than 25.
  • db.students.find({age: {$gte: 25}}) - Find all documents where the age is greater than or equal to 25.
  • db.students.find({age: {$lt: 25}}) - Find all documents where the age is less than 25.
  • db.students.find({age: {$lte: 25}}) - Find all documents where the age is less than or equal to 25.
  • db.students.find({name: {$in: ["Mridul", "Jane"]}}) - Find all documents where the name is either "Mridul" or "Jane".
  • db.students.find({$and: [{name: "Mridul"}, {age: 25}]}) - Find all documents where the name is "Mridul" and the age is 25.
  • db.students.find({$or: [{name: "Mridul"}, {age: 25}]}) - Find all documents where the name is "Mridul" or the age is 25.
  • db.students.find({$and: [{name: "Mridul"}, {age: {$gt: 25}}]}) - Find all documents where the name is "Mridul" and the age is greater than 25.

🔔 MongoDB Setup : 👉 How to Setup Mongo DB

🌿 Test MongoDB Connection in Local:

🌱 REST API using NodeJS and MongoDB :

🌱 More Learning Resources : ( 👉 Doc Link )

  • Todo App using MongoDb and Host in Vercel
  • Blog Website using MongoDb

🅱️ MongoDB Issues and Solutions

About

MongoDB Notes and Setup + REST-API using NodeJS and MongoDB

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published