Mongo-DB Basic Commands

MongoDB is a new type of DBMS.In here things are little different.(More details)

It is non-relational (data not stored in a tabular format)
    • It has collections not tables
    • It has Documents not records
    • A document is a Jason object
So in mongo db  collection ( A table in sql database) is a set of json objects called documents. Database is a set of collections.

Here are some basic commands used to work with mongodb
  • Change the database or create and change
    • use [database name]
      Don't have to worry about the database created or not. just have to use a name if it is not created.
      >>db will return the name of the current database.
      Ex: use people
      People is the name of the database here
  • Insert data into table
    • db.[collection name].insert([Json object])
      don't have to create collection (table). If exist it add record to the collection otherwise creates a new collection add a new record.
      Ex:
      
      db.people.insert(
                   "index": 0,
                   "isActive": true,
                   "balance": "$2,454.38",
                   "age": 34,
                   "name": "Lindsay Hancock",
                   "gender": "male",
                   "email": "lindsayhancock@netility.com",
                   "phone": "+1 (910) 518-3158",
                   "address": "240 Waldorf Court, Neibert, Northern Mariana Islands, 6509"
      
      )
      

      Entry is given an a json object
  • Get the collections in database
    • show collections
      This will display all the collections in database

  • View all the documents in a collection
    • db.[Collection name].find()
      Ex: db.people.find()

      Thes will show all the entries in Collection "people"

  • View all the documents in a collection in a organized way.
    • db.[Collection name].find().pretty()
      Ex:db.people.find().pretty()

  • Filter documents of a collection and display
    • db.[Collection name].find([Selection criteria])
      Ex: db.people.find({"age": 34})
      Selection criteria should be given as a json object

  • Remove a document from a collection
    • db.[Collection name].remove([Selection criteria])
      Ex: db.people.remove({"age": 34})
      This will remove all the documents which has age parameter as 34

  • Update a document in a collection
    • db.[Collection name].update([Selection criteria],[Json object])
      Ex:
      
      db.people.update({"name": "Lindsay Hancock"}, {
                   "index": 0,
                   "isActive": true,
                   "balance": "$2,454.38",
                   "age": 34,
                   "name": "Lindsay Hancock",
                   "gender": "male",
                   "email": "lindsayhancock@netility.com",
                   "phone": "+1 (910) 518-3158",
                   "address": "239 Jackson Street, Aguila, Delaware, 1494"
      
        })
      

      This will replace the document that has name "Lindsay Hancock" with the new json object

  • Delete an entire collection
    • db.[Collection name].drop()
      Ex: db.people.drop()
      This will delete the collection named as "people"
Those are the basic command needed to use mongoDB generally. but there are lot more to study about mongo to use it effectively.



No comments:

Post a Comment