MongoDB Part 3: Documents

In the previous article we had covered basics concepts of Mongo. In this article I will be talking more on working with documents (inserting,querying,deleting,updating) to start things off we must first go over the various data types in Mongo, which will all be similar to someone with experience in any standard SQL Database. Aside from your standard datatypes that we are all most likely accustomed too such as Strings, Integers and Booleans. Here are some more unique datatypes that are included with Mongo.

  • Regular Expression
  • JavaScript Code
  • Binary Data
  • Time stamp/Date

Inserting Documents

The syntax for insertion of documents is as simple as the last section. Once you have created a collection, you can insert documents by simply using this format.


If you wanted to add multiple rows you can insert an array of documents, and delimit each by a comma.

Querying Documents

In SQL, if we wanted to retrieve all the data in a table, it would look something like


The format for doing this same operation in MongoDB is simply.


Once you do this query, you will notice it might look a little awkward. A very neat feature in Mongo is by providing the .pretty() method after the find(), which will format the data in a pretty way!


The next set of operations deal with comparison in where clauses. This list orders the common set of operators along with its syntax inside a find clause.

  • Equal to : find({key:value});
  • Greater than : find({key:$gt:value});
  • Less than : find({key:$lt:value});
  • Greater than or equal : find({key:$gte:value});
  • Less than or equal : find({key:$lte:value});
  • Not equal : find({key:$ne:value});

The last two operators in classic where clauses are the AND / OR. In Mongo AND is very simply adding a comma inside the find method. ‘OR’ is a little different as you must surround the parameters with a ‘OR’ indicator $or

Here are some examples of both in use

AND Code Example


OR Code Example

$or: [

Update and Delete Documents

Much like its counterparts Mongo does provide support for updating and deleting entries in a database, in the event which one of the two would be needed.

Here is the basic syntax of a update statement, followed by a delete statement. Note that the one parameter is optional on the delete, as it indicates that only one document to be deleted instead of all of them

db.collection_name.update(SELECTION, UPDATED DATA);
db.collection_name.delete(DATA TO BE DELETED, one?);

Now this is what the statement would look like on actual data, for an update statement. If we wanted to update a date on an object for example.

db.collection_name.update({'Date Updated':'August 30'},
{$set:{'Date updated':'March 20'}},{multi:true})

Note the optional parameter which sets multiple documents to be updated.

Here is the syntax for Deletion of documents, with some data. Here I have not indicated just one document to be deleted, so it will delete all entries of discontinued books.

db.collection_name.delete("Title":"Discontinued Book")

What would happen if you provided no arguments with the delete command.. Then this would delete all entries in the database.

This concludes the third blog set of my Mongo Series. Next blog post will be discussing Java Database Connection (JDBC) with MongoDB.

Like what you read? Give gurbar.sidhu a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.