The four core database operations used in database driven application development are create,read,update and delete(CRUD).
Create
Create
This is one of the four basic database operations ,CRUD. create operations are those that add new records or documents to a collection in MongoDB. You can create documents in a MongoDB collection using any of the following basic operations:
If the collection does not exist, then the insert() method creates the collection during the first insert.
The update() operation in MongoDB accepts an “upsert” flag that modifies the behavior of update() from updating existing documents, to inserting data. These update() operations with the upsert flag eliminate the need to perform an additional operation to check for existence of a record before performing either an update or an insert operation. These update operations have the use <query> argument to determine the write operation:
The save() method updates an existing document or inserts a document depending on the_id field of the document. The save() method is equivalent to the update() method with theupsert option and a <query> argument on the _id field. The save() method has the following syntax:
Delete
The delete operations are those that remove documents from a collection in MongoDB.
The remove() method which is analogous to the DELETE operation in SQL is used to delete documents from a collection. The remove() method has the following syntax:
db.collection.remove( <query>, <justOne> )
If you do not specify a query, remove()removes all documents from a collection, but does not remove the indexes. If there is a <query> argument, the remove() method deletes from the collection all documents that match the argument.
Please refer here for more details.
- insert
- upsert
The insert() is the primary method to insert a document or documents into a MongoDB collection and is analogous to the INSERT statement in SQL. It has the following syntax:
db.collection.insert( <document> )
If the collection does not exist, then the insert() method creates the collection during the first insert.
The update() operation in MongoDB accepts an “upsert” flag that modifies the behavior of update() from updating existing documents, to inserting data. These update() operations with the upsert flag eliminate the need to perform an additional operation to check for existence of a record before performing either an update or an insert operation. These update operations have the use <query> argument to determine the write operation:
- If the query matches an existing documents, the operation is an update.
- If the query matches no document in the collection, the operation is an insert.
An upsert operation has the following syntax:
db.collection.update( <querry>,
<update>,
{ upsert: true } )
{ upsert: true } )
Read
The read operations include all operations that return a cursor in response to application request data. In the mongo shell, the methods that perform the read operations are find() and findone(). The syntax of find() operation is as follows:
db.collection.find( <query>, <projection> )
db.collection.find( { typr: 'food' }, { item: 1, qty: 1 } )
The operation finds all documents in the collection where type is food and the projection limits the output to the item, qty and _id field.
The read operations include all operations that return a cursor in response to application request data. In the mongo shell, the methods that perform the read operations are find() and findone(). The syntax of find() operation is as follows:
db.collection.find( <query>, <projection> )
- The db.collection object specifies the database and collection to query. All queries in MongoDB address a single collection.
- The <query> argument of the find() method holds this query document. A read operation without a query document will return all documents in the collection.
- The <projection> argument describes the result set in the form of a document. Projections specify or limit the fields to return.
For example, consider the following find() operation:
db.collection.find( { typr: 'food' }, { item: 1, qty: 1 } )
The operation finds all documents in the collection where type is food and the projection limits the output to the item, qty and _id field.
The findone() method has the same syntax as that of find() but returns only a single document instead of the cursor.
Update
Update operations are those that modify existing records or documents in a MongoDB collection. The methods to perform update operations are:
Update operations are those that modify existing records or documents in a MongoDB collection. The methods to perform update operations are:
- update
- save
The update() method corresponding to the UPDATE operation in SQL has the following syntax:
db.collection.update( <query>, <update>, <options> )
The update() method is the primary method used to modify documents in a MongoDB collection. By default, the update() method updates a single document, but by using the multi option, update() can update all documents that match the query criteria in the collection. The update() method can either replace the existing document with the new document or update specific fields in the existing document.
db.collection.save( <document> )
Delete
The delete operations are those that remove documents from a collection in MongoDB.
The remove() method which is analogous to the DELETE operation in SQL is used to delete documents from a collection. The remove() method has the following syntax:
db.collection.remove( <query>, <justOne> )
If you do not specify a query, remove()removes all documents from a collection, but does not remove the indexes. If there is a <query> argument, the remove() method deletes from the collection all documents that match the argument.
Please refer here for more details.
Comments
Post a Comment