Home > Articles > Web Development

Performing CRUD Operations with MongoDB

  • Print
  • + Share This
Jesse Smith shows you how Create, Read, Update, and Delete (CRUD) operations with MongoDB are easy using Mongo Query methods with JSON-like syntax.
Like this article? We recommend

The SQL query language has been used for many years to manage database data. All the major database engines, such as Oracle, SQL Server, and others not only implemented standard SQL but also added their own enhancements for a more programmatic procedural implementation (i.e., TSQL, PSQL). All this was done even before the Internet got to be popular or mainstream.

The Internet paved the way for disparate applications on various systems at different locations to communicate with each other using web services. With web services, protocols were needed to transport the objects between applications in text form over the wire (i.e., SOAP, XML).

Although XML worked to fulfill this need, it was hard to use and it used a lot of bandwidth. All the major database engines started to support storing XML in table columns as a new XML data type. New query languages, such as XPath and XQuery, went into action to query the data in these columns.

In effect, these new languages were query languages for XML, based on SQL. It was just a matter of time before a new data format for transporting data over the wire would come on the scene to address some of the XML shortcomings.

The new data-format mechanism was called JSON, or JavaScript Object Notation. Many of the newer web services use only JSON instead of XML as the data transport. You would expect the big database players to add JSON support to their engines, and this was the case as of Oracle 12c. However, at the time of this writing, SQL Server still has no support for JSON.

Low and behold: Enter MongoDB, a database that stores JSON and allows you to query JSON data documents using a special query language. A great fit for web services and RESTful web-based applications and frameworks such as AngularJS.

As you will see, the syntax for a managing data through this query language is very similar to that of Object Relational Mapping (ORM) frameworks such as JPA and Entity SQL. Like those frameworks, Mongo uses find and limit methods, among others.

Although it is not official, I like to refer to a Mongo query, which is any query against a MongoDB, as MQL or Mongo Query Language. MongoDB is a “No-SQL” database, which means it doesn't use relational data concepts; it is referred to as a document storage database. Nonetheless, there has to be a way to query the data in any type of database. Hence the need for a query language such as MQL.

Inserting Data

After installing MongoDB from mongodb.org, one of the first things you should know is how to insert data into the database. MongoDB already comes with a database called test, which is the database this article uses to demonstrate queries.

To get started (and assuming that MongoDB is started on your machine and the Mongo shell is open), type the following at the prompt:

j = { name : “Jacks Teller”, age: 36 }

Mongo refers to the line above as a document. The name/value pairs use the JSON format syntax. If you type j at the prompt, it returns the line you typed, but in complete JSON format, as follows:

{ “name” : “Jacks Teller”, “age”:36}.

The data or document you entered above still has to be inserted into the database. To do this, type the following:


You can also do an insert this way:

db.testData.insert({ name : “Jacks Teller”, age: 36 })

The testData part of the statement above is referred to as a collection. You can think of a collection as a table in a relational database or an array of locations to hold documents. The testData collection now has a document stored in it.

You can also think of a document as a record. To view a list of collections in your database, type the command show collections at the prompt.

How do you add nested values? Suppose that for the document above, the user Jack belongs to a couple of groups. You can do this:

db.testData.insert({ name : “Jacks Teller”, age: 36, groups: [“SOA”,“Mayans”]})
  • + Share This
  • 🔖 Save To Your Account