MongoDB CRUD stands for Create, Read, Update, Delete. MongoDB provides rich semantic to communicate with the database. MongoDB CRUD operations are much similar to SQL create,select,update and delete operations. If you already work with SQL you will find it very easy to understand and if your not then still you will find it very easy! OK, before moving forward to CRUD operation we have to understand how MongoDB stores data. As we know MongoDB is document oriented database it stores the data in the form of documents.
So no rows and columns. It uses JSON(JavaScript Object Notation) like format to store data. Formally, MongoDB documents are BSON documents. BSON is a binary representation of JSON with additional type information. following image is example of MongoDB document.
In MongoDB there are collections instead of table. In MongoDB database contains number of collections MongoDB stores all the documents in collection. Collection is a group of related documents that have a set of shared common indexes.
We will perform the tasks in following order :
- Create
- Insert
- Read
- Update
- remove
1. Create:
lets start from creating database in MongoDB. Now what you have to do is just enter following command on mongo shell,
Syntax:
use databasename
Example:
use blogdata
Database is not created unless you not inserting documents in it.
2. Insert:
To write into database following syntax is used,
Syntax:
db.collectionname.insert({ document to be inserted});
You can use any other folder name instead of "db" if you want.You don't have to create collection separately. It will automatically get created.
example:
To see the document that you have inserted run this command "db.collectionname.find()", "collectionname" should be the same which you have used earlier to insert document.
3. Read:
To read data from the database following command is used,
Syntax:
db.collectionname.find();
By using this command documents will get displayed, you can be more specific by writing some criteria in find(), by default it will display all documents that contains in collection. But if you want to get specific document then Mongo provides way to write more optimized query you can use :
- Limit the cursor means you can limit the number of result you get which matches criteria.
- Can add projections means you can specify which fields you want in result.
- Can add query criteria
Example:
This query returns documents in which age is greater than 18. It will display only name and address field because it is specified as "1" if field is "0" then mongo does not display that fields in result. Look at following SQL query which gives same result as mongo query for more better understanding.
4. Update:
Update operation is used to modify the documents,
Syntax:
db.collectionname.update({ criteria})
Example:
Following query will update the document field "status" as "A" where field "age" has value greater than "18".
Following SQL query gives the same result as mongo.
5. Remove:
Remove operation is used to delete documents from database,
Syntax:
db.collectionname.remove({criteria})
Example:
Following query will remove the documents where "status" field is "D".
See the SQL query for same operation,
Microsoft Office 2019 Crack is an updated and complete office packaged software with all the latest, old, upcoming, and other office tools. Office 2019 Crack
ReplyDelete