Wednesday, March 28, 2018

MongoDB for dummies


MongoDB WTF?????





  • Most important word NOSQL.
  • This doesn't mean as the same, it means that not only SQL.
  • Distribution , Horizontal scalability and Easy replication is prominent when comes to NOSQL.
  • NOSQL databases helped for the rise of Big data, Machine Learning technologies.

  •  MongoDB records are called documents.
  •  Each MongoDB database (You can have many.) includes collections, which are a set of JSON documents.
  •  Each collection and document has an ObjectID created by MongoDB or supplied by the programmer.
  • For SQLees or "SQL people",
    • Table is like a collection
    • Row/Record is like a document
    • No keys PK,FK

  • For installation refer this

  • Create a database
use Products
switched to db Products
  • Create a collection
db.createCollection("users")


    • Insert an item
    db.users.insert([
    {
        name:"John",
        email:"john@gmail.com"
    }
    ])

      Here the items are added in object form. To many more than one item an array can be used.

      • To Select results from db use,
        • db.getCollection('users').find({})



      /* 1 */
      {
          "_id" : ObjectId("5ae6b03fd26140c6db274889"),
          "name" : "bandula",
          "email" : "asdsa@gmail.com"
      }

      /* 2 */
      {
          "_id" : ObjectId("5ae6b099d26140c6db274892"),
          "name" : "John",
          "email" : "john@gmail.com"
      }

      • For querying parameter can be passed as objects
        • db.getCollection('users').find({name:"bandula"})
      /* 1 */
      {
          "_id" : ObjectId("5ae6b03fd26140c6db274889"),
          "name" : "bandula",
          "email" : "asdsa@gmail.com"
      }

      • Remove a document 
        • db.getCollection('users').remove({name:"john"})
      condition to be checked for the removal is passed as object
      • Update a document
        • db.getCollection('users').updateOne({name:"bandula"},{$set:{email:"bandula@hotmail.com"}})
      condition to be checked, updating parameters are passed. updating parameters are passed in the $set object 





      No comments:

      Post a Comment