Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Tutorials
  3. A Beginner’s Guide to MongoDB Documents

A Beginner’s Guide to MongoDB Documents

A document is the basic unit of data in MongoDB. It’s comprised of field/value pairs which are written in BSON format. A field is the name/value pair in a MongoDB document while a value is the specific value of a document’s field/value pair. A group of documents together is known as a collection.

Fields

Fields in a MongoDB document can be of any BSON data type. The full list of types can be found here.

The most important thing to remember is that each MongoDB document requires a unique _id field, which serves as the primary locator key for each document.

If the user doesn’t specify an _id field when creating a new document, MongoDB automatically creates ObjectId value represented as a string of numbers. This is shown in the examples below:

ObjectId (default)

{
     _id: ObjectId("57d28452ed5d4d54e8687098"),
     first: "Hugh",
     last: "Manatee",
     email: "[email protected]",
}

The _id field is immutable, however you can add a new document with an _id field of any BSON data type other than array.

An example of a custom ID (in this example, string)

{
     _id: "Studio3TMascot",
     first: "Hugh",
     last: "Manatee",
     email: "[email protected]",
}

The maximum document size is 16 MB, however MongoDB uses GridFS to accommodate larger documents.

Insert Document

To insert a document in mongo Shell, input the insertOne() command. The syntax is as follows:

db.manatees.insert(
       { 
          _id: "West Indian Manatee"
       }
)

To insert multiple documents use insertMany() instead:

db.manatees.insert( 
       {
          ["West Indian Manatee", "Amazonian Manatee"]
       }
)

Query a Document

To query a document in MongoDB, input the find() command. This command will return every document that contains the specified keyword.

db.manatees.find(       
       {    
          _id: "West African Manatee"
       }
)

Querying multiple documents with the AND condition is expressed as ‘,’ while the OR condition is expressed as $or. These can also be used together, as seen here:

db.manatees.find(
       {
          _id: "West African Manatee", "Amazonian Manatee" 
          $OR: "West Indian Manatee"    
       }
)

Large databases often return many documents with each query.  Organizing these documents is done with the pretty() cursor method.  For more info check out MongoDB’s official documentation.

Update Document

Updating the values in a document is done through the update() command, as shown here:

db.manatees.update(
       {
          _id: "West Indian Manatee" 
       },
       {
          population: 6101
       }
)

Updating multiple documents at once requires the ‘multi’ parameter.  The $set opratator allows the user to update specific fields within documents without changing anything else:

db.manatees.update(
       {
          likesMangroveLeaves: false  
       },
       {
           $set:
           {
               likesMangroveLeaves: true
           }
       },
       {
          multi:true
       }
)

Delete Document

Documents are deleted in mongo Shell with the remove() command.

To remove all documents of a specific type, specify the _id.

db.manatees.remove(
       { 
          _id: "elephant seals"
       }
)

To ensure only one document of a type is deleted, use the justOne parameter:

db.manatees.remove(
       {
          _id: "elephant seals"
       },
       {
          justOne: true
       }
)

Lastly, to remove all documents in a collection, simply refrain from specifying any deletion criteria:

db.manatees.remove(
       {
       }
)
Updated on December 5, 2019

Was this article helpful?

Related Articles