This topic describes the compatibility changes in MongoDB 3.6.
To view the official MongoDB compatibility change documentation, visit Legacy Documentation.
Localhost binding compatibility changes
Starting from MongoDB 3.6, mongod and mongos binary files are bound to localhost by default. If the net.ipv6 parameter is set in the configuration file or IPv6 support is enabled through the --ipv6 command line option, these binary files are bound o the IPv6 address of the localhost.
Starting from MongoDB 2.6, only binary files from the official MongoDB RPM packages (for Red Hat, CentOS, Fedora Linux, and their derivatives) and DEB packages (for Debian, Ubuntu, and their derivatives) are bound to localhost by default.
When bound only to localhost, MongoDB 3.6 binary files only accept connections from clients (including the mongo shell, members of replica set instances, or nodes in sharded cluster instances) on the same machine. Remote clients cannot connect to binary files that are only bound to localhost.
To override the default behavior and bind binary files to other IP addresses, use the net.bindIp parameter in the configuration file or the --bind_ip command line option to specify a list of hostnames or IP addresses.
Before binding to a non-localhost (such as a publicly accessible IP address), ensure that you have secured your cluster against unauthorized access. To secure your cluster, enable authentication and harden your network infrastructure.
For example, the following mongod instance is bound to the localhost and the hostname My-Example-Associated-Hostname. The host name is associated with the IP address 198.51.100.1.
mongod --bind_ip localhost,My-Example-Associated-HostnameTo connect to this instance, remote clients must specify the hostname or its associated IP address 198.51.100.1.
mongo --host My-Example-Associated-Hostname
mongo --host 198.51.100.1Sharded cluster instances
Starting from MongoDB 3.6, shards must use the replica set architecture. To upgrade a sharded cluster instance to MongoDB 3.6, all shard servers must be running as replica sets.
HTTP API and REST API
Starting from MongoDB 3.6, MongoDB removes the HTTP API and REST API.
Configuration | mongod/mongos option |
|
|
|
|
|
|
|
|
Tool updates
MongoDB 3.6 removes the mongooplog tool.
Array operation compatibility changes
$type: "array" behavior changes
Starting from MongoDB 3.6, the type: "array" and $type: 4 expressions match array fields that contain any elements type.
Prior to MongoDB 3.6, $type: "array" only matches array fields that contain nested arrays.
For example, a collection named c contains the following documents:
{ "_id": 1, "a": [ 1, 2, 3 ] },
{ "_id": 2, "a": [ 1, 2, [ 3, 4 ] ] }The following operation queries data by the type of field a:
db.c.find( { "a": { $type : "array" } } )Starting from MongoDB 3.6, a $type query returns the two documents in the collection because the query now detects that field a is an array.
{ "_id": 1, "a": [ 1, 2, 3 ] },
{ "_id": 2, "a": [ 1, 2, [ 3, 4 ] ] }Prior to MongoDB 3.4, a $type query only returns documents where field a contains elements of the BSON array type.
{ "_id": 2, "a": [ 1, 2, [ 3, 4 ] ] }If your current instance runs MongoDB 3.4.x and you are using partial indexes whosepartialFilterExpression contains $type: "array" or $type: 4, you must rebuild these indexes after an upgrad to MongoDB 3.6, thereby avoiding conflicts due to semantic changes in $type: 'array'.
Array sorting behavior changes
Starting from MongoDB 3.6, when sorting on an array field, MongoDB uses the lowest-valued array element for an ascending sort key and the highest-valued array element for a descending sort key. A sort no longer considers query predicates when selecting an array element as a sort key. This behavior change applies to the find command and aggregation pipeline operations.
Consequently, applications that sort on an array field may return different sorting results.
As a result of further adjustments to sorting behavior by an array field in MongoDB 4.4, when you sort on an array field with a multikey index, a query plan includes a blocking sort stage unless:
The index bounds of all sort fields are
[MinKey, MaxKey].No boundaries for any multikey-indexed field have the same path prefix as the sort pattern.
find method sorting behavior changes
In MongoDB, a sort key is an array element that MongoDB uses during the sorting process to compare documents with array fields. In an ascending sort, documents containing arrays with the lowest-valued sort keys are ordered first. In a descending sort, documents containing arrays with the highest-valued sort keys are ordered first.
Differences in sorting behavior between versions:
Prior to MongoDB 3.4: a sort on an array field considers query predicates when determining its sort key.
Starting from MongoDB 3.6: A sort on an array field no longer considers query predicates when determining its sort key. Instead, the sort key is the lowest- or highest-valued element in the array.
If your application is upgraded to MongoDB 3.6 sorts on an array field and uses query predicates, you must verify whether the sorting logic is affected by the behavior change and adjust your code to ensure compatibility.
Example:
Assume that a collection named coll contains the following documents:
{ _id: 0, a: [-3, -2, 2, 3] }
{ _id: 1, a: [ 5, -4 ] }Execute a query and sort operation for the array field a:
db.coll.find({a: {$gte: 0}}).sort({a: 1});In MongoDB 3.6, the sort key is the lowest-valued element in the array (ignoring query predicates):
The lowest-valued element in array
afor the document with_id: 0is -3.The lowest-valued element in array
afor the document with_id: 1is -4.
The following result is returned in ascending order:
{ "_id" : 1, "a" : [ 5, -4 ] } { "_id" : 0, "a" : [ -3, -2, 2, 3 ] }Prior to MongoDB 3.4, the sort key is the lowest-valued element that matches the query predicate
{$gte: 0}:The matching elements for the document with
_id: 0are[2, 3], and the lowest-valued element "2" is used as the sort key for the document.The matching element for the document with
_id: 1is[5], and the lowest-valued element "5" is used as the sort key for the document.
The following result is returned in ascending order:
{ _id: 0, a: [-3, -2, 2, 3] } { _id: 1, a: [ 5, -4 ] }
Aggregation method sorting behavior changes
Starting from MongoDB 3.6, when you use db.collection.aggregate() to sort on an array field, only a single element in the array is used as the sort key.
Example:
// Documents.
{ "_id" : 1, "timestamps" : [ ISODate("2017-07-15T15:31:01Z"), ISODate("2017-07-21T18:31:01Z") ] }
{ "_id" : 0, "timestamps" : [ ISODate("2017-07-21T15:31:01Z"), ISODate("2017-07-21T13:31:01Z") ] }
// Query.
db.c.aggregate([{$sort: {timestamps: -1}}])In MongoDB 3.6:
For a descending sort, the most recent time in the array is used as the sort key:
3:31 PM on July 21 for the document with
_id: 0, and5:31 PM on July 21 for the document with
_id: 1.
The sort is in descending order. Therefore, these keys are ordered from most recent to least recent, resulting in the document with
_id: 1sorting before the document with_id: 0.
Prior to MongoDB 3.6, the entire array is used as the sort key for aggregation sorts. The array sort keys are compared element-wise to determine the sort order of a result set.
// Documents.
{_id: 0, a: [3, 1, 5]}
{_id: 1, a: [3, 4, 0]}
// Query.
db.coll.aggregate([{$sort: {a: 1}}])Prior to MongoDB 3.6, the sort keys are [3,1,5] and [3,4,0] respectively.
The first array elements in the two keys are the same (3). Then, the second array elements are compared (
1 < 4).Consequently, the document with
_id: 0is sorted before the document with_id: 1.
Compound sorting limits on multiple array fields in aggregation pipeline
Starting from MongoDB 3.6, when sorting in an aggregation pipeline, MongoDB can no longer sort documents containing parallel arrays in sort files. Arrays are considered parallel if they are sibling elements of the BSON object. Arrays are not considered parallel in the following situations:
Sort keys contain nested arrays, such as field paths with hierarchical relationships.
Sort keys share the same array as a path prefix.
These limits have always existed in the find command. However, find and aggregate share the same semantics in MongoDB 3.6 and later versions.
Example 1: A sort succeeds if a nested array exists.
A collection contains the following documents:
{a: [ {b: [1, 2]}, {b: [3, 4]} ]}Execute the following aggregation operation (sort key is a nested array field):
db.coll.aggregate([{$sort: {"a.b": 1}}])The operation succeeds because
a.bis a nested array field and does not constitute parallel arrays.Example 2: A sort succeeds if sort keys share the same array as a prefix.
A collection contains the following documents:
{a: [{b: 1, c: 1}, {b: 2, c: 2}]}Execute the following aggregation operation (sort keys share the same array as a prefix):
db.coll.aggregate([{$sort: {"a.b": 1, "a.c": 1}}])The operation succeeds because
a.banda.cshare the prefixaand are not considered parallel arrays.Example 3: Sibling sort keys cause a failed sort.
A collection contains the following documents:
{ _id: 1, a: [ 1, 2 ], b: [ 1, 2 ]} { _id: 2, a: [ -3, 5 ], b: 0 } { _id: 3, a: [ -6, 12 ], b: 100 }Execute the following aggregation operation (sort keys are sibling array fields):
db.coll.aggregate([ { $sort: {a: 1, b: 1} } ])MongoDB cannot sort on both the
aandbfields because they are sibling arrays and constitute parallel arrays in the document with_id: 1. To perform a compound sort on multiple array fields, ensure that the sort fields are nested arrays or share the same array as a prefix, and avoid using array types in sibling fields in your data model.
Update operation updates
New field updates
Starting from MongoDB 3.6, fields added during update operations are appended in lexicographic order.
For example, a collection named coll contains the following document:
{ _id: 0, x: 0 }Execute an update operation to add the following fields:
db.coll.update({_id: 0}, {$set: {b: 0, a: 0}})In MongoDB 3.6, new fields are appended in lexicographic order. Consequently, the updated document is {_id: 0, x: 0, a: 0, b: 0}.
Prior to MongoDB 3.6, new fields are appended in order of their appearance in the update document. Consequently, the updated document is {_id: 0, x: 0, b: 0, a: 0}.
Fields conflicting with arrayFilters identifier syntax
Starting from MongoDB 3.6, fields whose name conflict with arrayFilters identifier syntax (such as $[]) can no longer updated.
For example, a collection named coll contains the following document:
{ _id: 0, x: { "$[]": 0 } }Execute an update operation:
db.coll.update({_id: 0}, {$set: {"x.$[]": 1}})Starting from MongoDB 3.6, an update operation succeeds. In MongoDB 3.6, an update operation fails because the field name $[] conflicts with arrayFilters identifier syntax.
This new update behavior only applies when featureCompatibilityVersion is set to 3.6.
Stricter validation of the $pop operator
Starting from MongoDB 3.6, the parameters of the $pop operator must be set to one of the following values:
-1: removes the first element of an array, or1: removes the last element of an array.
Prior to MongoDB 3.6:
Negative numbers to remove the first element of an array, or
Non-negative numbers or non-numeric values to remove the last element of an array.
Removal of the $pushAll operator
MongoDB 3.6 removes the $pushAll operator (deprecated since MongoDB 2.4).
Use the $push and $each operators instead. Example:
db.students.update(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)Platform support changes
MongoDB 3.6 no longer supports Windows versions prior to Window Server 2008 R2 and Windows 7.
MongoDB 3.6 is not tested on APFS, the new filesystem in macOS 10.13+ and may encounter compatibility issues.
General compatibility updates
Deprecation of the MongoDB-CR authentication mechanism
Starting from MongoDB 3.6, MongoDB deprecates the MONGODB-CR authentication mechanism. If you have not, upgrade your MONGODB-CR authentication schema to SCRAM.
Arbiter nodes and their priority
All arbiter nodes have a fixed priority of 0.
Deprecation of master-slave replication architecture
MongoDB 3.6 officially deprecates the master-slave replication architecture.
Deprecation of the --nojournal option for the WiredTiger storage engine
MongoDB 3.6 deprecates the --nojournal option for replica set members that use the WiredTiger storage engine, thereby avoiding data risks.
Adjustments to the aggregate command and its output
Starting from MongoDB 3.6, the aggregate command no longer returns results as a single document.
When running the aggregate command, you must specify one of the following options:
cursor: returns results as a cursor (suitable for large datasets).explain: returns an analysis of the execution plan for an aggregation pipeline.
We recommend that you use db.collection.aggregate() in the mongo shell or the equivalent method in your driver. These methods return a cursor by default unless the explain option is used.
Changes to conversion from date fields to strings in aggregation expressions
Starting from MongoDB 3.6, MongoDB forcibly converts a date to a string that contains millisecond precision and end with the letter 'Z' (indicating UTC timezone) in an aggregation expression.
Example:
// Documents.
{_id: 0, d: ISODate("2017-10-18T20:04:27.978Z")}
{_id: 1, d: ISODate("2017-10-18T20:04:28.192Z")}
// Query.
db.coll.aggregate({$project: {d: {$toLower: "$d"}}})Prior to MongoDB 3.6, data fields in the two documents are returned as strings in the "2017-10-18t20:04:27" and "2017-10-18t20:04:28" format.
Starting from MongoDB 3.6, data fields in the two documents are returned as strings in the "2017-10-18t20:04:27.978z" and "2017-10-18t20:04:28.192z" (including milliseconds and lowercase 'z') format.
The changes apply to the following aggregation operators:
$concat$substr$substrBytes$substrCP$strcasecmp$toLower$toUpper
Removal of log diagnostics commands and options
MongoDB 3.6 removes deprecated diagLogging and mongod --diaglog option.
Instead, use the mongoreplay tool to capture, replay, and analyze commands sent to your MongoDB deployment.
validate operation changes
Starting from MongoDB 3.6, a validate operation forcibly triggers a checkpoint and flushes all in-memory data to disk only before executing a validation for disk data.
Prior to MongoDB 3.6, a checkpoint is forcibly triggered regardless of the used validation mode.
When using the validate command or the db.collection.validate() method, explicitly specify {full: true} to perform a full validation.
Index name limits
Starting from MongoDB 3.6:
You cannot specify
*as the index name during index creation.You cannot use index keys to delete indexes named
*. Use the index name*to delete the indexes instead.
Before upgrading to MongoDB 3.6, you must:
Delete indexes: Manually delete all existing indexes named
*.Rename indexes: To keep the indexes, delete them and rebuild them with new names.
Deprecated options
Changes in MongoDB 3.6.1:
Deprecate the
snapshotquery option.MMAPv1 storage engine: Use
hint({ _id: 1 })instead to ensure that documents are not returned multiple times during a query even though intermediate writes cause documents to be moved.Other storage engines (such as WiredTiger): Use
hint({ $natural: 1 })instead.
Deprecate the
$isolatedoperator.Alternative: Use transactions or adjust read concern levels to achieve isolation.
Features incompatible with later versions
The following new features in MongoDB 3.6 require that featureCompatibilityVersion is set to "3.6:
UUID for collections: Each collection has a unique identifier.
$jsonSchemadocument validation: supports a document structure validation in JSON Schema format.Change Streams: monitors data change events in real time.Chunk aware secondaries: optimizes data synchronization on secondary nodes in a sharded cluster instance.
View definitions, document validator, and partial index filter: If using new query features in MongoDB 3.6, you must enable this configuration.
Sessions and retryable writes: supports transaction sessions and automatic retry of write operations.
Authentication restrictions for users and roles: manages user permissions in a fine-graine manner.
The default values of featureCompatibilityVersion:
New deoployments of MongoDB 3.6:
"3.6".Deployments upgraded from MongoDB 3.4:
"3.4"(requires the use of setFeatureCompatibilityVersion).
You can use the command db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) to view the current featureCompatibilityVersion value.
To downgrade from MongoDB 3.6, you must clear all incompatible data, including persistent data related to sessions, change streams, and UUID for collections.