MongoDB
The Query API.
Simple, expressive and unified.
A flexible, intuitive way to work with data. Deliver applications with minimal code and maximum productivity, in the language and tool of your choice.
The MongoDB Query API gives developers an intuitive way to handle complex data workloads for any data type, including arrays, geospatial, and time series data, so they can deliver more with less code.
- Work in your preferred language
- A single interface for operational, analytical, and search workloads
Work easily with any data type
Get complete control over your data with the MongoDB Query API. It’s designed to handle any data structure with ease, including key-value, graph, geospatial, time series, and objects. Optimized for embedded arrays and objects, these expressive querying capabilities unlock the flexibility of the document model.
Boost productivity with a single interface
Unlock the full potential of data operations. Perform key operations, including indexing, querying, and updating; all through a single, powerful API. Modernize your applications by using transactional, search, and analytical queries, without the need to switch between interfaces or struggle with complex query syntax.
Feature overview
Expressive querying
Use a wide range of powerful operators, like $elemMatch and $geoWithin to query and update one or more documents based on a combination of criteria, including within nested arrays and objects.
Diverse index types
Optimize your queries with many index types — no matter how deeply nested the fields.
Geospatial and graph data
Query geospatial data easily and use operators, like $graphLookup, to traverse connected datasets.
Query across collections and databases
Use $lookup and $unionWith to join and blend multiple collections, or Atlas Data Federation to query across databases, for deep analysis and exploration.
Transform + analyze data in place
Use the aggregation pipeline to build complex transformations in an easy, modular fashion.
Full-text search built for the document model
Deploy advanced, full-text search — without the need to manage additional infrastructure.
Change streams
Build real-time, event-driven apps that react to database changes by triggering server-side logic.
On-demand, incremental materialized views
Pre-compute and cache common queries, dynamically updating result sets as new data is processed.
Simplify working with data
Expressive querying made easy
xxxxxxxxxx
#Querying on arrays
db.inventory.find({
“Size.h”: {$lt: 15}, “size.uom”: “in”, status: “D”
})
#Geospatial queries
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
Composable in-database transformations
xxxxxxxxxx
lonely_ne = db.lonely_neighborhoods
popular_ne = db.popular_neighborhoods
pipeline = [
#filter neighborhoods where the population is known
{"$match": {"population": {"$ne": "unknown"}}},
#perform a union with the collection containing popular neighborhoods with an embedded pipeline for filtering neighborhoods where the population is known, and removing documents where the city is an industrial township
{"$unionWith": {"coll": "popular_neighborhoods",
"pipeline": [
{"$match": {"population": {"$ne": "unknown"}}},
{$redact: {$cond: {
if: { $eq: [ "$type", "Industrial township"]},
then: "$$PRUNE",
else: "$$DESCEND" }}
}
] }},
#finally we are grouping neighborhoods by climate to calculate the total population in popular neighborboods that are not in an industrial township
{"$group": {"_id": "$climate", "totalPopulation": {"$sum": {"$toLong": "$population"}}}}
]
result = lonely_ne.aggregate(pipeline)
Full-text search, time series, and more
xxxxxxxxxx
#running a full-text search query
movies = db.movies
pipeline = [
{"$search": {"near": {"path": "released", "origin": {"$date": "2011-09-01T00:00:00.000+00:00"}, "pivot": 7776000000}}}},
{"$project": {"_id": 0, "title": 1, "released": 1}}
]
result = movies.aggregate(pipeline)
#analyzing time series with window functions
cakeSales = db.cakeSales
pipeline = [
{"$setWindowFields": {
"partitionBy": {"$year": "$orderDate"},
"sortBy": {"orderDate": 1},
"output": {"cumulativeQuantityForYear": {"$sum": "$quantity", "window": {"documents": ["unbounded", "current"]}}}
}
}
]
result = cakeSales.aggregate(pipeline)
for res in result:
print(res)
Query. Transform. Analyze.
Tinker with an interactive demo app
Start using the Query API today
- CRUD
- Aggregations
- Change streams
- Geospatial and graph
- Full-text search
- Language drivers