Performing CRUD Operation

View Databases and Collections

If you are new to MongoDB, it may be helpful to understand a couple of MongoDB database concepts:

  • A record in MongoDB is a document, which is a data structure composed of field and value pairs, similar to JSON objects. The value of a field can include other documents, arrays, and arrays of documents. A document is roughly equivalent to a row in a relational database table.
  • A collection in MongoDB is a group of documents, and is roughly equivalent to a relational database table.
  • A database in MongoDB is a group of collections, and is similar to a relational database with a group of related tables.

Use the below command to view the available databases in the your Amazon DocumentDB cluster:

show dbs

The show command doesn’t list any databases because no databases have been created yet. To create a database in DocumentDB, execute the use command, specifying a database name. Create a new database called catalog.

use catalog

When you create a new database in DocumentDB, there are no collections created for you. You can see this on your cluster by running the following command.

show collections

To create a collection, either execute the createCollection command or perform a CRUD operation against the collection. If the collection doesn’t exist yet, DocumentDB will create it, then perform the CRUD operation.

Creating Documents

You will now insert a document to a new collection called products in your catalog database using the below query.

db.products.insert({
"name" : "RayBan Sunglass Pro",
"sku" : "1590234",
"description" : "RayBan Sunglasses for professional sports people",
"inventory" : 100
})

Amazon DocumentDB sends an acknowledgement as shown below, indicating the number of documents inserted

Amazon DocumentDB CRUD 1

You will now insert multiple documents in a single batch to bulk load products. The insertMany command below loads 5 more product documents. Note that one document does not have a description field. Document databases provide flexibility to store data and do not require a schema on write.

db.products.insertMany([
{
"name" : "GUCCI Handbag",
"sku" : "3451290",
"description" : "Fashion Hand bags for all ages",
"inventory" : 75
},
{
"name" : "Round hat",
"sku" : "8976045",
"inventory" : 200
},
{
"name" : "Polo shirt",
"sku" : "6497023",
"description" : "Cool shirts for hot summer",
"inventory" : 25
},
{
"name" : "Swim shorts",
"sku" : "8245352",
"description" : "Designer swim shorts for athletes",
"inventory" : 200
},
{
"name" : "Running shoes",
"sku" : "3243662",
"description" : "Shoes for work out and trekking",
"inventory" : 20
}
])

The output for this query returns the ObjectId of the documents inserted. NOTE: ObjectId is generated by Amazon DocumentDB so you will see different values in your query response.

Amazon DocumentDB CRUD 2

Reading Documents

Use the below query to read data inserted to Amazon DocumentDB. The find command takes a filter criteria and returns the document matching the criteria. The pretty command is appended to display the results in an easy-to-read format.

db.products.find({"sku":"1590234"}).pretty()

The matched document is returned as the output of the above query.

Amazon DocumentDB CRUD 3

When no arguments are passed to the find API, the first 20 documents in the collection will be returned. Optionally, you can choose to return only specified fields in the documents.

Updating Documents

You will now update a document to add reviews using the $set operator with the update command. Reviews is a new array containing review and rating fields.

db.products.update(
{"sku":"1590234"},
{
$set: {	
"reviews" : [{
"rating" :4,
"review":"perfect glasses"
},{
"rating" :4.5,
"review":"my prized possession"
},{
"rating" :5,
"review":"Just love it"
}]}
}
)

The output indicates the number of documents that were matched, upserted, and modified.

Amazon DocumentDB CRUD 4

You can read the document modified above to ensure that the changes are applied.

db.products.find({"sku":"1590234"}).pretty()

Amazon DocumentDB CRUD 5

Multiple documents can be updated by passing {"multi": true} as a third parameter to the above update function with appropriate filter condition in the first parameter.

Deleting Documents

You can delete a document using the below code.

db.products.remove({"sku":"8976045"})

The output indicates the number of documents removed:

Amazon DocumentDB CRUD 6