RavenDB vs MongoDB
RavenDB works for you
MongoDB makes you work
Discover the power of RavenDB.
A Powerful, Secure, and Easy-to-Use NoSQL Database.
Why choose
RavenDB over MongoDB
Great default experience
-
Indexes are created automatically with every query.
-
Comes with authentication out of the box.
-
Good installation defaults mean less configuration later.
Handles the complexity of data management
-
You get intuitive SQL-like query language, standard JSON document format, and server-side patching.
-
Don’t need to look up docs for simple queries, or worry about serializing your BSON documents.
Scalable and performant
-
Queries are faster as they run on automatically created indexes, not raw data.
-
Client-side caching to make things ever faster.
-
Multi-master architecture makes choosing new cluster leaders instant when nodes fail.
Great default experience
-
Indexes are created automatically with every query.
-
Comes with authentication out of the box.
-
Good installation defaults mean less configuration later.
Handles the complexity of data management
-
You get intuitive SQL-like query language, standard JSON document format, and server-side patching.
-
Don’t need to look up docs for simple queries, or worry about serializing your BSON documents.
Scalable and performant
-
Queries are faster as they run on automatically created indexes, not raw data.
-
Client-side caching to make things ever faster.
-
Multi-master architecture makes choosing new cluster leaders instant when nodes fail.
RavenDB and MongoDB were both started around 2009 and on the surface, they are quite similar. Document databases using JSON with focus on performance, scalability and a modern approach for database design.
When you look beyond the surface, it turns out that they have very different design philosophies and vision about what a modern database should be. In this page, we'll discuss the differences between RavenDB and MongoDB, and their impact on your systems.
Main points
Queries
Indexes
Design philosophies
Reduce complexity of working with data, at any scale.
RavenDB focuses heavily on making things easy for developers and operators. RavenDB takes it upon itself to handle the complexity of data management for you.
RavenDB takes the approach that you don't need an expert at hand.
A core tenet of RavenDB's design philosophy is that you should be focused on getting things done, and the database engine will do the Right Thing for you.
Backend for extremely large-scale, cloud-based systems.
MongoDB focuses on enterprise solutions with high configurability and a high degree of flexibility, aiming to provide an answer to almost any scenario.
The drawback of this approach is that to operate a MongoDB database properly, you need a high degree of dedicated expertise to achieve good results.
For that reason, MongoDB recommends MongoDB Atlas (the cloud-hosted version) instead of running MongoDB directly.
Queries
The first query
from Orders
where ShipTo.City = 'London'
RavenDB uses a SQL dialect, intended to be familiar and intuitive for everyone. The query language was extended to allow document operations while keeping the ease of use and familiarity of SQL.
db.Orders.find({
"ShipTo.City": "London"
})
MongoDB queries are using a JSON-based format specific to the database. You'll need to familiarize yourself with the language to effectively use MongoDB.
Efficient queries
No action is required on your part to ensure that the query will run fast.
The RavenDB query optimizer knows how RavenDB works and uses your workload to derive the best indexes for your queries automatically.
db.Orders.createIndex({
"ShipTo.City": 1
})
Requires careful index management for good performance.
With MongoDB, it is the responsibility of the developers (or database administrators) to create the right set of indexes for the queries.
This is a delicate and complex task that requires deep insight into both the system and the database internals. Without careful attention to detail, it is easy to end up with very slow queries.
Fixing these sorts of issues can be costly and complicated.
Queries evolution over time
Getting indexes right is a chore, but the real hurdle is when you need to evolve your system. When a new requirement is raised, how do we handle it?
Let's add sorting to the previous query, and see...
from Orders
where ShipTo.City = 'London'
order by OrderedAt desc
limit 10
The work is completed
// RavenDB adjusts itself for
// the new query on the fly
db.Orders.find({
"ShipTo.City": "London"
}).sort({ "OrderedAt": -1})
.limit(10)
Some assembly required
db.Orders.createIndex({
"ShipTo.City": 1,
"OrderedAt": -1
})
Aggregating data
Aggregating data is a common operation in most systems. It is also one of the most expensive operations you can perform in a database, processing vast amounts of data.
Let's explore how each database handles aggregation.
from Orders
group by ShipTo.City
order by count() desc
select ShipTo.City, count()
RavenDB goes the extra mile here, beyond the simplicity of the query. The query optimizer will synthesize an index such that the results will be near instantaneous, regardless of data size.
db.Orders.aggregate([{
"$group": {
"_id": "$ShipTo.City",
"Count": { "$sum": 1 }
}
},
{ "$sort": { "Count": -1 } },
{
"$project": {
"City": "$_id",
"Count": 1,
"_id": 0
}
}])
In MongoDB, this query runtime is proportional to the amount of records, even if you have the appropriate index.
The more Orders you have, the more costly this query is.
Non-trivial queries
We have looked at very simple queries so far.
Let's see what happens as the application evolves and we look at more complex scenarios.
from Orders
where (ShipTo.City = 'London' or
ShipTo.Country = 'Narnia')
and OrderedAt between $weekAgo and
$today
and Status in (’Ready’, ’Pending’)
order by Total desc
limit 10
RavenDB's queries are natural to read and easy to write, even as the queries grow more complex. RavenDB leverages your existing knowledge, and the query optimizer has your back to ensure great performance.
db.Orders.find({
$or: [
{ "ShipTo.City": "London" },
{ "ShipTo.Country": "Narnia" }
],
"OrderedAt": {
$gte: _weekAgo
$lte: _today
},
"Status": { $in: ["Ready", "Pending"] }
})
.sort({ Total: -1 })
.limit(10)
MongoDB's queries quickly become unwieldy to manage except for the most trivial of queries.
MongoDB, beyond the more complex challenge, offers additional challenges. The query above touches several fields in the document.
Even if you created the right indexes, MongoDB will often use just a single index for the query. MongoDB's query optimizer is occasionally able to use multiple indexes to optimize a query, but MongoDB documentation recommends against relying on this behavior.
Indexes
Indexes allow a database engine to answer your queries in milliseconds instead of hours. RavenDB and MongoDB both use indexes to speed up queries, but there are a lot of differences between them.
Auto indexing
- RavenDB's query optimizer monitors your queries on an ongoing basis and creates or removes indexes on the fly based on actual production usage.
- Expertise level: None, fully automated.
- MongoDB depends on the database administrator to manage the set of indexes required for the application.
- MongoDB Atlas (a managed cloud offering) has a recommendation feature for new indexes but requires operators to manually create them.
- Expertise level: High, manual operation only.
Indexes performance & scalability
RavenDB processes indexes in the background, meaning that writes can complete successfully without having to wait for the indexes to run. This behavior enables RavenDB to run several important optimizations that can massively increase the indexing speed.
Users can choose on a case-by-case basis whether to wait for the indexes to finish running. The same applies to reads, resulting in a highly performant system without having to do any waits.
RavenDB supports any number of indexes and is able to perform reliably with hundreds of them in place, with no issues.
Indexes in MongoDB are processed as part of the document write operation. The more indexes you have, the slower your write performance becomes. On the other hand, without an appropriate index, your queries will suffer.
MongoDB has a limit of up to 64 indexes per collection. That is a hard limit, regardless of the system resources. Hitting this limit means that you need to re-think your entire persistence strategy.
Managing the right indexes in MongoDB is a complex process, balancing query speed vs. write performance and requiring detailed knowledge of the internals of the database engine.
Index flexibility & usability
The manner in which each database can use its indexes is very different. Consider the following queries, common when users can choose their own sorting criteria.
from Employees
order by LastName, FirstName
from Employees
order by LastName desc, FirstName
RavenDB will use a single index to answer both queries (the query optimizer will create it for you if needed).
Being able to reuse a single index for many tasks will significantly reduce the workload on your database.
RavenDB's indexes are built in such a way that a single index can be used for many different queries. This significantly reduces the resources that RavenDB needs to handle your workload.
Any field in the index can be queried and sorted individually or in combination with other index fields, meaning that a single index in RavenDB serves double or even triple duty compared to indexes in MongoDB (or even relational databases).
db.Employees.find()
.sort({ LastName: 1,
FirstName: 1 })
db.Employees.find()
.sort({ LastName: -1,
FirstName: 1 })
db.Employees.find()
.sort({ LastName: 1, FirstName: 1 })
db.Employees.find()
.sort({ LastName: -1, FirstName: 1 })
MongoDB will need two separate indexes to answer those queries efficiently, doubling the cost of writes.
db.Employees.createIndex({
LastName: 1, FirstName: 1 })
db.Employees.createIndex({
LastName: -1, FirstName: 1 })
The structure of an index in MongoDB means that sort order matters. It's easy to write a query that uses an index and later make a simple change (such as the sort direction), which will force MongoDB to perform a full collection scan (a very slow operation).
As usual, a high degree of expertise, care, and attention is required to build effective applications using MongoDB.
RavenDB's indexes are built in such a way that a single index can be used for many different queries. This significantly reduces the resources that RavenDB needs to handle your workload.
Any field in the index can be queried and sorted individually or in combination with other index fields, meaning that a single index in RavenDB serves double or even triple duty compared to indexes in MongoDB (or even relational databases).
The structure of an index in MongoDB means that sort order matters. It's easy to write a query that uses an index and later make a simple change (such as the sort direction), which will force MongoDB to perform a full collection scan (a very slow operation).
As usual, a high degree of expertise, care, and attention is required to build effective applications using MongoDB.
Aggregations
- RavenDB can move aggregation costs into the background, pre-computing the required information and massively speeding query time.
- RavenDB's Map-Reduce indexes allow queries to return near-real-time data instantly, regardless of the data size involved.
- MongoDB can utilize indexes during aggregation queries, but still needs to execute the aggregation at query time.
- As the size of the data grows, the slower such queries become. Optimizing aggregation queries in MongoDB is a task for experts.
Full text search
- Fully supported out of the box. No third party integration and no special work required.
from Posts
where search(Title, 'awesome')
- RavenDB full text search allows search, faceting, typo correction, highlights, geo-spatial queries, more-like-this, custom analyzers, etc.
- You can use the same database nodes to hold your data and to search on it, no need for dedicated machines for that purpose.
- MongoDB has only very basic support for full-text search, with many limitations (performance, capabilities, and usage scenarios).
- A typical solution is to integrate Elasticsearch to enable full-text search, including continuously updating the search index.
- MongoDB recommends using MongoDB Cloud with Atlas Search for users needing full-text search. This is not available for local deployments and incurs additional costs and significant architectural complexity.
- MongoDB also recommends deploying dedicated search nodes at high volumes.
Indexing computed values
- RavenDB allows you to compute values during indexing, such as deriving the total value of an order by summing up its line items.
- You can also integrate additional services, such as machine learning, during the indexing process.
- RavenDB allows you to integrate with package managers to run custom code during the indexing process easily.
- MongoDB has no support for computing values during the indexing process.
- MongoDB recommends that you write computed fields directly into your documents (and ensure that they are updated on every write) instead of computing them at indexing time.
Operations
Securing your data
Your database contains the most valuable parts of your system. Protecting your data is a primary concern for your business.
RavenDB is secured by default. There are no post-install steps required to run in a secured manner and in fact RavenDB will actively refuse to run if you try to run in an unsecured mode.
RavenDB uses X.509 certificates to authenticate both clients and servers and operates under the Zero Trust assumption that the network is always hostile. All data is encrypted in transitand can optionally be encrypted at rest.
Security with RavenDB is simple, effective, and proven. There are no special actions or complex configuration to understand and setup. If RavenDB runs, it is running securely.
RavenDB makes it very hard to run it in an unsecured manner.
MongoDB offers a wide variety of authentication and authorization integrations. However, you need to opt in for any of these, as by default, MongoDB will run without any authentication whatsoever. See: How to lose your data in 3 steps
It's easy to end up in situations where your production database is completely open to the whole wide world. Ransomware attacks targeting MongoDB have affected tens of thousands of instances, and any public instance is attacked within minutes.
Setting up MongoDB securely requires reading a 100+ page document and properly following all recommendations. The proper procedure is complex and error-prone, leading to many instances remaining unprotected and potential data loss.
Distributed work and sharding
RavenDB and MongoDB can both run in a cluster, for scalability and high availability reasons. They take a very different approach on the actual mechanics of that.
RavenDB uses the multi-master model, in which different nodes can share the load of both reads and writes. The cluster will assign roles and manage the workload on its own, with no operator involvement required.
Nodes in RavenDB cluster are homogenous, requiring no special treatment or complex topologies. There is a single global configuration for the entire cluster.
For truly large databases, RavenDB recommends going to a sharded solution when your database grows beyond the 5 TB range.
MongoDB uses the single primary and multiple secondary nodes model. Read operations can be distributed across the cluster, but writes must be directed to the single primary node.
A MongoDB cluster is composed of many distinct components (config servers, query routers, and shards). Each one needs to be deployed and configured separately.
MongoDB write scaling requires moving to a sharded approach as soon as your database approaches the 200 GB mark, which is far earlier than with RavenDB.
Failover & high availability
RavenDB and MongoDB both offer automatic failover when an error occurs. There are practical and important differences in the way the databases handle failure.
RavenDB was designed to expect and deal with failures transparently to the user. The multi-master architecture means that any node can accept writes and reads.
The primary node going down will be detected and a new leader will be selected in under one second.Clients will immediately fail to the next node transparently to your application.
There is no action required by developers to enable automatic failover, nor is there a need to configure anything on the server side. RavenDB's design expects failures to happen, and ensures that your application remains at full capacity even then.
Primary node failure means downtime for writes for a long duration (more than 10 sec by default). In some conditions, recovery from the failure will take much longer.
By default, reads from the cluster fail if the
primary node is down. You need to specify the appropriate
readConcern
setting on the call for read failover to work.
Proper failover handling requires cooperation from both the database administrators and developers and necessitates careful testing to ensure high availability with MongoDB.
Patching documents & migration
Your applications grow over time, and data models change.
Let's see how each database allow you to modify your documents and evolve their structure.
from Employees as emp update {
emp.Name = emp.LastName + ”,
“ + emp.FirstName;
delete emp.FirstName;
delete emp.LastName;
}
RavenDB allows you to modify your JSON documents en masse using JavaScript. Any type of change is possible.
Such update operations are executed directly inside the database engine, with no network traffic required.
RavenDB also supports JsonPatch
to perform
partial updates.
// not possible, you need to write
// a full blown program for this
MongoDB's update capabilities are limited to the
updateMany()
call, which can update multiple documents in one call.
Such a method does not allow you to make changes to each document based on its own data, but only set properties to the same value across all documents.
A migration operation such as unifying
FirstName
and LastName
intoName
requires writing
code to load the documents into your process, modify them, and save them back.
Operational overhead
RavenDB does a lot behind the scenes to make your applications more performant, easier to develop and handle full production load with no hassles.
RavenDB will automatically cache all read operations for you, leading to lower latency (and frees you from needing to manage caching, cache invalidation, etc).
RavenDB is always transactional, keeping your data safe. Beyond keeping your data, it also offers Full Text Search and advanced querying capabilities as a built-in feature.
Automatic indexes and modern indexing structure let you focus on your application, instead of on query optimization.
Built-in monitoring, alerts and performance hints in RavenDB reduce the need for high expertise and allow you to go to production with confidence.
MongoDB requires a significant amount of complexity to run successfully in production. The assumption is that the database is being run by a team that is very familiar with how it works.
MongoDB leaves caching and cache management completely at the hands of the user. You are expected to use a 3rd party tool or roll your own if you need the performance boost from the cache.
MongoDB has transaction support since 2019, but as an opt-in feature. By default, it may persist partial data. Full Text Search requires either integrating with Elasticsearch. Such integration requires additional setup, synchronization, and maintenance.
MongoDB relies heavily on indexes for query performance. Administrators must manually analyze query patterns, create appropriate indexes, and periodically optimize them.
Incorrect indexing can lead to performance bottlenecks.
Open source
RavenDB is an Open Source project with an OSI approved permissive license. Fully compatible with the Open Source definition.
MongoDB used to be Open Source, but changed the license in 2018 to a Source Available. MongoDB is no longer an Open Source project.
MongoDB was meant to be a backend database for a Platform as a Service product.
RavenDB was envisioned to be a database that lets developers get things done.
MongoDB was meant to be a backend database for a Platform as a Service product.
RavenDB was envisioned to be a database that lets developers get things done.
Thousands of customers have chosen RavenDB
See how we compare to other databases
Get started in minutes
Sign up or launch a playground and see the difference for yourself