Couchbase Server

Comparing MongoDB MQL with N1QL features in Couchbase 6.5

This is a short note reviewing the MongoDB MQL language features highlighted in the release blog: MongoDB 4.4: User-Driven Engineering. Ready for You.  MongoDB 4.4 release has added a number of features for the MQL language. Couchbase released 6.5 earlier the year with a significant number of features in N1QL — both for query service and analytics service. Let’s dive in.

MongoDB 4.4 Couchbase 6.5
Released GA: July 2020 GA: January 2020
What’s new? https://www.mongodb.com/collateral/mongodb-4.4-guide https://docs.couchbase.com/server/current/introduction/whats-new.html
UNION set operation Introduces $UnionWith aggregation pipeline stage to bend data into a single
result set. Roughly equivalent to UNION ALL set operation in SQL.
https://docs.mongodb.com/master/reference/operator/aggregation/unionWith/#pipe._S_unionWith
Supports all SQL set operations since the initial release: UNION, UNION ALL,
EXCEPT, EXCEPT ALL, INTERSECT, INTERSECT ALL. The ALL modifier returns duplicate
documents. Using the operation without ALL avoids the sort stage required in
the MQL aggregation pipeline. These set operations can be used anywhere a
SELECT query is used (subqueries, derived tables, etc).
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/union.html
https://blog.jooq.org/2015/10/06/you-probably-dont-use-sql-intersect-or-except-often-enough/
Custom Aggregation Expressions Introduces $function, allowing JavaScript snippets inside the aggregation
pipeline to customize processing. User-defined aggregations can be created
using $accumulate, $function, $merge, and $group. This approach is verbose
and must be rewritten for every query. In SQL, once a user-defined aggregate
is created, it can be reused like any other aggregate.
Other improvements include simple scalar functions.
https://docs.mongodb.com/master/release-notes/4.4/#custom-aggregation-expressions
Preview release of SQL functions and JavaScript functions usable anywhere
expressions are allowed in N1QL. These can be part of complex queries and are
expected to be GA in the next major release.
MongoDB still lacks window functions, which were released in Couchbase 6.5
and significantly improve developer productivity.
Couchbase supports JavaScript-based aggregation via map-reduce views, though
they are not accessible through N1QL.
Couchbase Eventing functions are also written in JavaScript and provide a
scalable infrastructure for modern applications.
Search Introduces full-text $search based on Lucene, available only in MongoDB Atlas.
MongoDB itself has limited search functionality and is not Lucene-based.
$search can only be used as the first stage of an aggregation pipeline and
cannot be used in updates or deletes.
Couchbase 6.5 introduced the SEARCH() function and related metadata functions.
It supports simple string search to full-text search with deep integration
into the N1QL optimizer.
Transactions Introduces the ability to create collections and indexes within a
non-sharded transaction. Using DDLs inside transactions is still rare.
Released distributed transactions for Java SDKs, allowing applications
to operate on multiple documents using get/set APIs. Distributed
transactions are not supported for N1QL statements.

Resources:

  1. Couchbase docs
  2. N1QL Tutorial
  3. MongoDB docs
Share this article
Get Couchbase blog updates in your inbox
This field is required.

Author

Posted by Keshav Murthy

Keshav Murthy is a Vice President at Couchbase R&D. Previously, he was at MapR, IBM, Informix, Sybase, with more than 20 years of experience in database design & development. He lead the SQL and NoSQL R&D team at IBM Informix. He has received two President's Club awards at Couchbase, two Outstanding Technical Achievement Awards at IBM. Keshav has a bachelor's degree in Computer Science and Engineering from the University of Mysore, India, and has received twenty four US patents.

Leave a comment

Ready to get Started with Couchbase Capella?

Start building

Check out our developer portal to explore NoSQL, browse resources, and get started with tutorials.

Use Capella free

Get hands-on with Couchbase in just a few clicks. Capella DBaaS is the easiest and fastest way to get started.

Get in touch

Want to learn more about Couchbase offerings? Let us help.