Mongo DB

Mongo DB

It helps to store big data, helps to maintain the scalable data. It is not a relational database. Document based DBMS. It stores the data in the form of BSON.

In Mongo DB , Column  referred as Document, Row referred as field, Table referred as collection

In Big data 3 type are there

  1. Structured Data, Have Schema. (SQL, MySQL)
  2. Un Structured Data, Doesn’t Have schema. Data come any form.
  3. Semi structured Data(Mix of Structured Data, Un Structured Data)

Commands

  1. Listing the database
    show dbs
  2. Selecting the database
    use databasename
  3. Create collection
    db.createCollection(‘profile’) — Create new collection
    db.createCollection(‘profile’).insert{{“Name”:”Uday”,”age”:”25″}} — Create new collection along with data
  4. Listing Collections
    show collections
  5. Dropping Collections
    db.collectionname.drop()
  6. Dropping Database
    db.dropDatabase()
  7. Finding the collection data
    i) db.collectionname.find()
    ii) db.collectionname.find().pretty() — helps to display JSON form
    iii) db.collectionname.find({Name:”uday”})
    iv) db.collectionname.find({Age:{$gt:38}}) — Numeric condition for greater than
    gt – greater than
    gte – greater than equal to
    lt – less than
    lte – less than equal to
    eq – equal to
    v) db.collectionname.find({$and:[{Name:”Uday”},{Age:23}]})
    and
    or
    vi) db.collectionname.find({Name:{$in:[“Uday”,”Kumar”]}})
    in
    nin
    vii) db.collectionname.find({age:{$not:{$lt:38}}})
  8. Sorting
    db.collectionname.find().sort({“Age”:1})
    1 – ascending
    2 – desending
  9. Projection
    db.collectionname.find({Name:”Ben”,{“City”:1,”_id”:0}})
  10. Delete
    db.profile.deleteOne({Name:”Uday”})– Delete with condition, If duplication fields are there, It deletes only first occurrence
    db.profile.delete({}) — delete all records
    db.profile.remove() — Delete the data even the duplicate fields.
    db.profile.remove({Name:”Uday”},1) — Tell the occurrence to remove
  11. ObjectId in collection
    Mongodb by defaultly creates the unique id for the collections. It acts as a primary key. Object id have 12 bytes, In 4 bytes created by timestamp, 3 bytes created by machine identifier, 2 bytes created by  process id, 3 bytes created by incremental. We can change object id generate mechanism. But it is not recommeded.
  12.  Getting collection count
    db.collectionname.find().count()
  13. Insert data in collections
    i) db.collectionname.insertOne({Name:”Uday”,City:”Madurai”})
    ii) db.collectionname.insertMany([{Name:”Uday”,City:”Madurai”},{Name:”Kumar”,City:”Madurai”}])
  14. Update data
    db.collectionname.update({Name:”Uday”},{$set:{City:”Bangalore”}})
    if we are not using set keyword, it will overwrite entire field