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
- 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"
No comments:
Post a Comment