Mongo Db - D B M S

Theory


  • Basic
    • Compass => To deal with Database in GUI form
    • ATLAS (Managed MongoDB Hosting | Database-as-a-Service) => Database on the Cloud
    • mongod (Mongo Daemon) => Host process for the database
    • mongo => Command-line shell that connects to a specific instance of mongod
    • Structure
      • SQL => Database, Tables, Rows, Column
      • MySQL (Mongo DB) => Database > Collections (JS Objects) > Documents (BSON) > Fields
  • Install
    • Search Install MongoDB > Click on Install MongoDB community edition > Click on Get MongoDB
    • Software > Community Server > Download
    • Install
    • Add its bin path to Environmental Variables (C:\Program Files\MongoDB\Server\4.4\bin) > Now you can access it through PowerShell
  • Mongo Compass
    • Connect to Local MongoD
      • Fill in connection fields individually > Connect
    • To create a Database
      • After starting Server > Create Database > Write Database Name & Collection Name > Create Database
      • Click on DatabaseName > Click on CollectionName > Add Data > Insert Document > Write your data > Insert

Basic Commands


  • mongosh --version => Check monosh Version
  • mongosh "YOUR_CONNECTION_STRING" --username YOUR_USER_NAME => Start the Mongo Shell
  • db; => Show Current Database
  • show dbs => Show All Databases
  • use blog => Create Or Switch Database
  • db.dropDatabase(); => Drop Database
  • db.createCollection("posts"); => Create Collection
  • show collections => Show Collections
  • Insert Document
      db.posts.insertOne({
          title: "Post 1",
          body: "Body of post.",
          category: "News",
          likes: 1,
          tags: ["news", "events"],
          date: Date(),
      });
    
  • Insert Multiple Documents
      db.posts.insertMany([
          {
              title: "Post 2",
              body: "Body of post.",
              category: "Event",
              likes: 2,
              tags: ["news", "events"],
              date: Date(),
          },
          {
              title: "Post 3",
              body: "Body of post.",
              category: "Tech",
              likes: 3,
              tags: ["news", "events"],
              date: Date(),
          },
      ]);
    
  • db.posts.find(); => Find All Documents
  • Find Documents with Query
      db.posts.find({
          category: "News",
      });
    

Sort Documents


  • Ascending
      db.posts.find().sort({
          title: 1,
      });
    
  • Descending
      db.posts.find().sort({
          title: -1,
      });
    

Filter Data


  • Count Documents
      db.posts.find().count();
      db.posts
          .find({
              category: "news",
          })
          .count();
    
  • db.posts.find().limit(2); => Limit Documents
  • Chaining
      db.posts.find().limit(2).sort({
          title: 1,
      });
    
  • Find One Document
      db.posts.findOne({
          likes: {
              $gt: 3,
          },
      });
    
  • Update Document
      db.posts.updateOne(
          {
              title: "Post 1",
          },
          {
              $set: {
                  category: "Tech",
              },
          }
      );
    
  • Update Document or Insert if not Found
      db.posts.updateOne(
          {
              title: "Post 6",
          },
          {
              $set: {
                  title: "Post 6",
                  body: "Body of post.",
                  category: "News",
              },
          },
          {
              upsert: true,
          }
      );
    
  • Increment Field ( $inc )
      db.posts.updateOne(
          {
              title: "Post 1",
          },
          {
              $inc: {
                  likes: 2,
              },
          }
      );
    
  • Update Multiple Documents
      db.posts.updateMany(
          {},
          {
              $inc: {
                  likes: 1,
              },
          }
      );
    
  • Rename Field
      db.posts.updateOne(
          {
              title: "Post 2",
          },
          {
              $rename: {
                  likes: "views",
              },
          }
      );
    

Delete


  • Delete a Document
      db.posts.deleteOne({
          title: "Post 6",
      });
    
  • Delete Multiple Documents
      db.posts.deleteMany({
          category: "Tech",
      });
    
  • Greater & Less Than
      db.posts.find({
          views: {
              $gt: 2,
          },
      });
      db.posts.find({
          views: {
              $gte: 7,
          },
      });
      db.posts.find({
          views: {
              $lt: 7,
          },
      });
      db.posts.find({
          views: {
              $lte: 7,
          },
      });
    
Share: