Skip to main content

MongoDB Operations

The four core database operations used in database driven application development are create,read,update and delete(CRUD).

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:
  • 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 } )

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> )
  • 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
  • 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.

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:
                            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

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Elements of Programming in Scheme

A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this. primitive expressions , which represent the simplest entities the language is concerned with, means of combination , by which compound elements are built from simpler ones, and means of abstraction , by which compound elements can be named and manipulated as units. Some very basic ideas in manipulating with the data are given below. Expressions   A very simple expression is a number. Simple expressions are combined to form complex ones. For example:                   (+ 2  3  4) is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:              ...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...