mongo旧版Shell的兼容性变化

此页面描述了 mongosh 和旧版 mongo shell 之间的区别。除了此处列出的备选方案外,您还可以使用 mongocompat 访问遗产的片段 mongo shellAPI。片段是一项实验性功能,有关更多信息,请参阅Snippets.

弃用的方法

以下 shell 方法在mongosh. 相反,请使用替代资源列中列出的方法。

弃用的方法 替代资源
db.collection.copyTo() 聚合阶段:$out
db.collection.count() db.collection.countDocuments()db.collection.estimatedDocumentCount()
db.collection.insert() db.collection.insertOne()db.collection.insertMany()db.collection.bulkWrite()
db.collection.remove() db.collection.deleteOne()db.collection.deleteMany()db.collection.findOneAndDelete()db.collection.bulkWrite()
db.collection.save() db.collection.insertOne()db.collection.insertMany()db.collection.updateOne()db.collection.updateMany()db.collection.findOneAndUpdate()
db.collection.update() db.collection.updateOne()db.collection.updateMany()db.collection.findOneAndUpdate()db.collection.bulkWrite()
DBQuery.shellBatchSize config.set("displayBatchSize", "")游标.batchSize()
Mongo.getSecondaryOk Mongo.getReadPrefMode()
Mongo.isCausalConsistency Session.getOptions()
Mongo.setSecondaryOk Mongo.setReadPref()
rs.secondaryOk 不再需要。看 读取辅助节点上的操作。

阅读偏好行为

从节点读操作

当使用遗留的 mongo shell 直接连接到 次要的副本集成员,您必须运行 mongo.setReadPref()以启用二次读取。

当使用mongosh直接连接到次要的 副本集成员,如果你指定一个,你可以从该成员读取 阅读偏好之一:

要指定阅读首选项,您可以使用以下任一方法:

当使用mongosh直接连接到次要的 副本集成员,如果您的读取首选项设置为 primaryPreferred,secondary或者 secondaryPreferred不需要运行 rs.secondaryOk()

show辅助方法

以下show帮助器方法始终使用 的读取首选项 primaryPreferred,即使已为操作指定了不同的读取首选项也是如此:

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

在旧版mongoshell 中,这些操作使用指定的读取首选项。

写入首选项行为

可重试写入中默认启用 mongosh。可重试写入在旧版中默认被禁用mongo壳。要禁用可重试写入,请使用 --retryWrites=false.

ObjectId 方法和属性

这些对象标识符()mongosh方法在传统shell 中的工作方式不同mongo

方法或属性 mongo行为 mongosh行为
ObjectId.str 返回十六进制字符串:6419ccfce40afaf9317567b7 不明确的(无法使用)
ObjectId.valueOf() 返回值ObjectId.str6419ccfce40afaf9317567b7 返回格式化字符串:ObjectId("6419ccfce40afaf9317567b7")
ObjectId.toString() 返回格式化字符串:ObjectId("6419ccfce40afaf9317567b7") 返回一个十六进制格式的字符串:6419ccfce40afaf9317567b7

数值

旧版shell默认mongo存储数值。doublesIn mongoshnumbers 存储为 32 位整数, Int32,或者就好像Double该值不能存储为 Int32.

MongoDB Shell 继续支持mongoshell 中支持的数字类型。但是,首选类型已更新以更好地与 MongoDB 保持一致司机. 有关详细信息,请参阅 mongosh 数据类型。

MongoDB Shell 中数字变量的首选类型与传统 shell 中建议的类型不同mongo。这些类型mongosh更好地与 MongoDB 驱动程序使用的类型保持一致。

mongo类型 mongosh类型
NumberInt Int32
NumberLong Long
NumberDecimal Decimal128

警告:

mongosh如果您同时使用旧shell连接到同一个集合,则数据类型的存储可能会不一致mongo

提示:

也可以看看:

有关管理类型的更多信息,请参阅 架构验证概述。

--eval行为

mongosh --eval不在其输出中引用对象键。

要获得适合自动解析的输出,请使用 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

用 解析后jq,输出类似于:

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

笔记:

EJSON内置了格式选项,可以消除对像jq. 例如,以下代码生成格式与上述相同的输出。

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

数据库调用的限制

数据库查询的结果不能在以下上下文中传递:

  • 类构造函数
  • 非异步生成器函数
  • .sort()数组回调

要访问数据库调用的结果,请使用异步函数, 异步生成器函数, 或者.map().

构造器

以下构造函数不起作用:

// This code will fail
class FindResults {
   constructor() {
      this.value = db.students.find();
   }
}
// This code will fail
function listEntries() { return db.students.find(); }
class FindResults {
   constructor() {
      this.value = listEntries();
   }
}

改用async函数

class FindResults {
   constructor() {
      this.value = ( async() => {
         return db.students.find();
      } )();
   }
}

笔记:

您还可以创建一个在类中执行数据库操作的方法,作为使用异步 JavaScript 的替代方法。

class FindResults {
  constructor() { }

  init() { this.value = db.students.find(); }
}

要使用此类,首先构造一个类实例,然后调用 .init()方法。

生成器函数

以下生成器函数不起作用:

// This code will fail
function* FindResults() {
   yield db.students.findMany();
}

// This code will fail
function listEntries() { return db.students.findMany(); }
function* findResults() {
   yield listEntries();
}

改为使用async generator function

function listEntries() { return db.students.findMany(); }
async function* findResults() {
   yield listEntries();
}

数组排序

以下数组排序不起作用:

// This code will fail
db.getCollectionNames().sort( ( collectionOne, collectionTwo ) => {
  return db[ collectionOne ].estimatedDocumentCount() - db[ collectionOne ].estimatedDocumentCount() )
} );

改用.map()

db.getCollectionNames().map( collectionName => {
   return { collectionName, size: db[ collectionName ].estimatedDocumentCount() };
} ).sort( ( collectionOne, collectionTwo ) => {
   return collectionOne.size - collectionTwo.size;
} ).map( collection => collection.collectionName);

这种数组排序方法通常比等效的不受支持的代码更高效。

翻译:韩鹏帅

原文:Compatibility Changes with Legacy mongo Shell

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""