Docs Menu

Compatibility Changes with Legacy mongo Shellicons/link.png

On this page

  • Deprecated Methods
  • Read Preference Behavior
  • Numeric Values
  • --eval Behavior

The following behaviors in mongosh affect compatibility with operations performed in the legacy mongo shell.

The following shell methods are deprecated in mongosh. Instead, use the methods listed in the Alternative Resources column.

Deprecated Method
Alternative Resources
db.collection.copyTo()
Aggregation stage: $out
db.collection.count()
db.collection.insert()
db.collection.remove()
db.collection.save()
db.collection.update()
DBQuery.shellBatchSize
Mongo.getSecondaryOk
Mongo.isCausalConsistency
Mongo.setSecondaryOk
rs.secondaryOk
No longer required. See Read Operations on a Secondary Node.

When using the legacy mongo shell to connect directly to secondary replica set member, you must run mongo.setReadPref() to enable secondary reads.

When using mongosh to connect directly to a secondary replica set member, you can read from that member if you specify a read preference of either:

To specify a read preference, you can use either:

When using mongosh to connect directly to a secondary replica set member, if your read preference is set to primaryPreferred, secondary or secondaryPreferred it is not required to run rs.secondaryOk().

The following show helper methods always use a read preference of primaryPreferred, even when a different read preference has been specified for the operation:

  • show dbs
  • show databases
  • show collections
  • show tables

In the legacy mongo shell, these operations use the specified read preference.

The legacy mongo shell stored numerical values as doubles by default. In mongosh numbers are stored as 32 bit integers, Int32, or else as Double if the value cannot be stored as an Int32.

MongoDB Shell continues to support the numeric types that are supported in mongo shell. However, the preferred types have been updated to better align with the MongoDB drivers. See mongosh Data Types for more information.

The preferred types for numeric variables are different in MongoDB Shell than the types suggested in the legacy mongo shell. The types in mongosh better align with the types used by the MongoDB Drivers.

mongo type
mongosh type
NumberInt
Int32
NumberLong
Long
NumberDecimal
Decimal128
Warning

Data types may be stored inconsistently if you connect to the same collection using both mongosh and the legacy mongo shell.

Tip
See also:

For more information on managing types, refer to the schema validation overview.

mongosh --eval does not quote object keys in its ouptut. To get output suitable for automated parsing, use EJSON.stringify().

mongosh --quiet --host rs0/centos1104 --port 27500 \
--eval "EJSON.stringify(rs.status().members.map( \
m => ({'id':m._id, 'name':m.name, 'stateStr':m.stateStr})));" \
| jq

After parsing with jq, the output resembles this:

[
{
"id": 0,
"name": "centos1104:27500",
"stateStr": "PRIMARY"
},
{
"id": 1,
"name": "centos1104:27502",
"stateStr": "SECONDARY"
},
{
"id": 2,
"name": "centos1104:27503",
"stateStr": "SECONDARY"
}
]
Note

EJSON has built in formatting options which may eliminate the need for a parser like jq. For example, the following code produces output that is formatted the same as above.

mongosh --quiet --host rs0/centos1104 --port 27500 \
--eval "EJSON.stringify( rs.status().members.map( \
({ _id, name, stateStr }) => ({ _id, name, stateStr })), null, 2);"
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.