AWS Database Blog
New Amazon DocumentDB features for aggregations, arrays, and indexing
Amazon DocumentDB (with MongoDB compatibility) is a fast, scalable, highly available, and fully managed document database service that supports MongoDB workloads. You can use the same MongoDB application code, drivers, and tools as you do today to run, manage, and scale workloads on Amazon DocumentDB. This way, you can enjoy improved performance, scalability, and availability without having to worry about managing the underlying infrastructure.
Today, Amazon DocumentDB added support for new aggregation pipeline operators and stages that allow you to compose powerful aggregations over your documents. The new capabilities include aggregation string operators ($concat
, $substr
, $substrBytes
, $substrCP
, $strcasecmp
), an array aggregation operator ($size
), an aggregation group accumulator operator ($push
), and aggregation stages ($redact
and $indexStats
). Additionally, Amazon DocumentDB now supports positional array operators ($[]
and $[<identifier>]
) for updating elements in an array and hint()
for selecting an index.
In this blog post, I introduce some of these new capabilities by showing common use cases. This way you can start using the capabilities to build and manage applications at-scale with Amazon DocumentDB.
Get started with Amazon DocumentDB
To get started with Amazon DocumentDB, you can use either the Amazon DocumentDB getting started guide, the quick start with AWS CloudFormation, or watch the following video. When you’re ready to start developing on Amazon DocumentDB, use the same application code, drivers, and tools that you’re using today with MongoDB.
New capabilities
After you create and connect to your Amazon DocumentDB cluster, you can better understand and extend the following examples.
Aggregation operators and stages
It’s common to store strings in documents such as first name, last name, or location. To make data processing easier and faster in your application, you might push manipulations such as joining two strings together, down to the database level. The new string aggregation operators that were added to Amazon DocumentDB enable you to manipulate strings for your specific use cases. Imagine you are building a human resources application and have a collection in Amazon DocumentDB that contains a number of documents for people in your organization.
Input
Each document in this example is for a single person and contains their first name, last name, date of birth, and a company specific identifier for where their desk resides.
$concat
The $concat
aggregation operator concatenates (or combines) multiple strings in a document to produce a single string that can be returned to the application. This reduces the work done in the application. In the following example, I concatenate the first and last names of users to produce each person’s full name.
Query:
Result:
With the use of $concat
aggregation operator, you can return the person’s full name back to the application so that it can be used without requiring application-level logic.
$substrCP
The $substrCP
operator returns a portion of a string (substring), where the desired substring is specified as a range of UTF-8 code points (CP). While the $substr
operator is useful for defining substrings as range of characters, and the $substrBytes
operator defines a substring as a range of bytes, $substrCP
is useful for defining substrings when you are not certain how many characters or bytes a given string element uses. This is common in Unicode strings. Let’s use the string “caffè latte” as an example as the use of the “è” highlights this point.
Let’s consider the following use of $substrCP: { $substrCP: [ "caffè latte", 0, 5] }
. The output would yield “caffè” as “caffè” is five code points from the beginning of the string (i.e., 0). If I used $subtrBytes
for a similar query { $substrBytes: [ "caffè latte", 0, 5] }
, I would get an error as it takes two bytes to store “è” and only returning the first byte of “è” won’t do anyone any good. Thus, to get the same result, I use the following query { $substrBytes: [ "caffè latte", 0, 6] }
. Using $substrCP
versus $subtrBytes
is preferred and simplifies your development. You don’t have to think about how many bytes it takes to store the characters in your strings.
Following from the previous example and using the collection of people in the organization, if I want to use $substrCP
to extract the state in which the employees reside, I use the following query.
Query:
Result:
From the encoding of each employee’s desk location, I know that the two character abbreviation for the state starts seven code points from the beginning of the string and is two code points long, i.e., { $substrCP: [ "$Desk", 7, 2] }
. Thus the results returned are the last two code points in the string. If I were using this type of encoding for the employees desk locations, I would ensure that I adhere to fixed lengths for each of the components. If not, it breaks the query that I just wrote.
$strcasecmp
Similarly, I can use the $strcasecmp
operator to perform a case-insensitive comparison between two strings, i.e., { $strcasecmp: [ string1, string2 ] }
. The output of the comparison tells us if the string2 is greater (1) than string1, equal (0) to string1, or less than (-1) string1.
Query:
Result:
From the people collection, if I compare “mke233-wi” with the desk locations of each of the employees, with the case insensitive $strcasecmp
operator, I should expect that the last entry (i.e., “_id”:3) to be equal to “MKE233-WI”.
For the array expression aggregation operator $size
and the aggregation group accumulator operator $push
, let’s consider a scenario where I build a user profile for a fantasy hockey league. Each user is able to select the teams they want to follow.
Input:
$size
I can use the $size
operator to return the count of items within an array field. In this scenario, let’s use $size
to return the count the number of teams each user follows.
Query:
Result:
$push
I can use the $push
aggregation group accumulator operator with the $unwind
and $group
aggregation stages to recategorize or pivot the data by teams and fans. Recategorizing the data can be useful for scenarios in which I want to target the fans of specific teams with a promotional email. In this case, the application logic would be straightforward. I would send the promotional email for the Sharks to “hockeyFan01” and “pondHockey12” versus having to either process every document and result email one-by-one, or do a collections scan for each team.
Query:
Result:
$redact
The $redact
aggregation stage is useful for scenarios of including and excluding content in documents based on the selected fields in the document. To better understand how to utilize this aggregation stage, let’s say I build a patient record system and I only want doctors with permissions to specific codes to be able to see patient records. If a doctor does not have access to a code, the patient records are pruned from the documents returned from the query.
Input:
Query:
Result:
Positional array operators
To understand the newly added positional array operators, let’s consider a use case where I use Amazon DocumentDB to store flight miles as part of an airlines rewards program.
Input:
Note: In Amazon DocumentDB, insertMany()
is an atomic operation, so either the entire command succeeds or it fails. Having atomicity here is useful as you do not have to worry about a failure occurring hallway through your insert and then trying to reconcile which documents were written to your database.
$[]
The positional array operator $[]
can be used to perform an operation on each of the elements in an array field. For example, an airline is recording, in an array, their customers’ flight miles from a given year. If a customer registers for a credit card with the airline, I give them a 100 percent bonus for the miles that they’ve already earned that year.
Query:
Result:
For customers that are credit card members, I can see that I doubled each of their flight miles.
$[<identifier>]
For the best customers that have been with the airline since 1980, I can run an additional promotion where I round up all flight miles less than 2,000 miles to 2,000 miles. To run that query in Amazon DocumentDB, I can use the $[<identifier>]
positional array operator.
Query:
Result:
For the two customers that had been with the airline since 1980s, I see from the result that because flight_miles “1205,” “880,” and “1780” were below the 2000-mile threshold that they were set to 2000 miles as part of the additional promotion.
Indexing
You can use the newly added indexing capabilities to control and manage your index selection and usage. If you have indexes that are not being used, either using them or removing them can go a long way to improving the performance of your database and ultimately your application.
hint()
In Amazon DocumentDB, the query processor evaluates and picks a query plan that maximizes for performance. At times, you might want to force the query processor to use a specific index. To do that, you can use the hint()
operator.
Input:
Let’s consider a use case where I store the inventory for a grocery store in Amazon DocumentDB. The grocery collection below contains the name of the product, category, pricing, and the quantity.
As a result of the input queries, I create a grocery collection and an index on the product field. I can use hint()
to ensure that the query processor uses the index that I created on the product field.
Query:
Result:
I can validate that the index on the product field was indeed used with the explain()
method, which provides us information about the query plan or how the query processor executed the query on the grocery collection.
Query:
Result:
From the output of explain()
, I can verify that query plan (i.e., stage) that produced the query results was an index scan (i.e., “IXSCAN”) that used “indexName”:”product_1” and thus the index was used query the documents in the grocery collection.
$indexStats
Following from the preceding grocery example, if I run a few queries on the indexes, I can then understand the operational use of these indexes using the $indexStats aggregation operator. While indexes help improve query performance, they also create additional work for the database when inserting data. Understanding how often your indexes are being used when querying the database helps you make more informed decisions on whether indexes are needed.
Query:
Result:
In this result, you can see that index “_id_” has been queried three times since it was created on 2019-02-19 or since the server last rebooted. Similarly, index “product_1” was queries seven times since it was created on the same date. For a production database, you can use $indexStats
to baseline and monitor your index usage over time.
Summary
With this Amazon DocumentDB release, we added new capabilities that enable you to build and manage applications at scale. For more information about Amazon DocumentDB, see the Amazon DocumentDB home page and getting started guide.
If you have any questions or comments about this blog post, please use the comments section on this page.
About the Author
Joseph Idziorek is a Principal Product Manager at Amazon Web Services.