Methods方法
On this page本页内容
Administration Methods管理方法Atlas Search Index MethodsAtlas搜索索引方法Bulk Operation Methods批量操作方法Collection Methods集合方法Connection Methods连接方法Cursor Methods游标方法Database Methods数据库方法Free Monitoring Methods免费监控方法Client-Side Field Level Encryption Methods客户端字段级加密方法Native Methods本机方法Query Plan Cache Methods查询计划缓存方法Replication Methods复制方法Role Management Methods角色管理方法Session Object Methods会话对象方法Server Status Methods服务器状态方法Sharding MethodsSharding方法Telemetry Methods遥测方法Transaction Methods事务处理方法User Management Methods用户管理方法
The following document lists the available methods in the MongoDB Shell. 以下文档列出了MongoDB Shell中可用的方法。Click a method to see its documentation in the MongoDB Manual, including syntax and examples.单击一个方法可以在MongoDB手册中查看其文档,包括语法和示例。
Methods which reset the shell's connection to the server trigger a termination of all open server sessions. 重置shell与服务器的连接的方法会触发所有打开的服务器会话的终止。When a session ends, all in-progress operations are terminated as well, and the client must re-authenticate if authentication is enabled.会话结束时,所有正在进行的操作也将终止,如果启用了身份验证,则客户端必须重新进行身份验证。
The following methods reset the shell's connection:以下方法重置外壳的连接:
db.auth
Mongo.setReadPref
Mongo.setReadConcern
Administration Methods管理方法
db.adminCommand() | admin database.admin 数据库运行命令。 |
db.currentOp() | |
db.killOp() | |
db.shutdownServer() | mongod or mongos process cleanly and safely.mongod 或mongos 进程。 |
db.fsyncLock() | |
db.fsyncUnlock() | db.fsyncLock() .db.fsyncLock() 锁定的数据库上继续写入。 |
Atlas Search Index MethodsAtlas搜索索引方法
Atlas Search indexes let you query data in Atlas Search. Atlas Search索引允许您在Atlas Search中查询数据。Atlas Search indexes enable performant text search queries by mapping search terms to the documents that contain those terms.Atlas Search索引通过将搜索术语映射到包含这些术语的文档来实现高性能文本搜索查询。
Use the following methods to manage Atlas Search indexes.使用以下方法管理Atlas Search索引。
The following methods can only be run on deployments hosted on MongoDB Atlas.以下方法只能在MongoDB Atlas上托管的部署上运行。
db.collection.createSearchIndex() | |
db.collection.dropSearchIndex() | |
db.collection.getSearchIndexes() | |
db.collection.updateSearchIndex() |
Bulk Operation Methods批量操作方法
db.collection.initializeOrderedBulkOp() | Bulk() operations builder for a collection. Bulk() 操作生成器。 |
db.collection.initializeUnorderedBulkOp() | Bulk() operations builder for a collection. Bulk() 操作生成器。 |
Bulk() | db.collection.initializeOrderedBulkOp() or the db.collection.initializeUnorderedBulkOp() method.db.collection.initializeOrderedBulkOp() 或db.collection.initializeUnorderedBulkOp() 方法。 |
Bulk.execute() | Bulk() operations builder.Bulk() 操作生成器生成的操作列表。 |
Bulk.find() | |
Bulk.find.hint() | |
Bulk.find.remove() | |
Bulk.find.removeOne() | |
Bulk.find.replaceOne() | |
Bulk.find.updateOne() | |
Bulk.find.update() | |
Bulk.find.upsert() | true for an update or a replacement operation.upsert 选项设置为true 。 |
Bulk.getOperations() | Bulk.execute() .Bulk.execute() 执行的写入操作的数组。 |
Bulk.insert() | |
Bulk.toJSON() | Bulk() object.Bulk() 对象中的操作数和批处理数。 |
Bulk.toString() | Bulk() object.Bulk() 对象中的操作数和批处理数。 |
Collection Methods集合方法
db.collection.aggregate() | |
db.collection.bulkWrite() | |
db.collection.count() | mongosh 1.0.6. mongosh 1.0.6中已弃用。db.collection.countDocuments() or db.collection.estimatedDocumentCount() instead.db.collection.countDocuments() 或db.collection.estimatedDocumentCount() 。 |
db.collection.countDocuments() | $group aggregation stage with a $sum expression.$sum 表达式包装$group 聚合阶段。 |
db.collection.estimatedDocumentCount() | |
db.collection.createIndex() | |
db.collection.createIndexes() | |
db.collection.dataSize() | size field in the output of the collStats .collStats 输出中的size 字段。 |
db.collection.deleteOne() | |
db.collection.deleteMany() | |
db.collection.distinct() | |
db.collection.drop() | |
db.collection.dropIndex() | |
db.collection.dropIndexes() | |
db.collection.ensureIndex() | db.collection.createIndex() .db.collection.createIndex() 。 |
db.collection.explain() | |
db.collection.find() | |
db.collection.findAndModify() | |
db.collection.findOne() | |
db.collection.findOneAndDelete() | |
db.collection.findOneAndReplace() | |
db.collection.findOneAndUpdate() | |
db.collection.getIndexes() | |
db.collection.getShardDistribution() | |
db.collection.getShardVersion() | |
db.collection.insertOne() | |
db.collection.insertMany() | |
db.collection.isCapped() | |
db.collection.mapReduce() | |
db.collection.reIndex() | |
db.collection.renameCollection() | |
db.collection.replaceOne() | |
db.collection.stats() | collStats .collStats 的包装。 |
db.collection.storageSize() | storageSize field of the collStats output.collStats 输出的storageSize 字段的包装。 |
db.collection.totalIndexSize() | totalIndexSize field of the collStats output.collStats 输出的totalIndexSize 字段提供包装。 |
db.collection.totalSize() | |
db.collection.updateOne() | |
db.collection.updateMany() | |
db.collection.validate() | |
db.collection.watch() |
Connection Methods连接方法
Mongo() | mongo shell or from a JavaScript file.mongo shell或JavaScript文件实例化数据库连接。Mongo() method has the following parameters:Mongo() 方法具有以下参数:
| |||||||||
Mongo.getDB() | ||||||||||
Mongo.setReadPref() | ||||||||||
Mongo.watch() | admin , local , and config databases.admin 数据库、local 数据库和config 数据库除外)。 |
Cursor Methods游标方法
cursor.addOption() | |
cursor.allowPartialResults() | db.collection.find() operations against a sharded collection to return partial results, rather than an error, if one or more queried shards are unavailable.db.collection.find() 操作以返回部分结果,而不是错误。 |
cursor.batchSize() | db.myCollection.find().batchSize(100) |
cursor.close() | |
cursor.collation() | db.collection.find() .db.collection.find() 返回的游标的排序规则。 |
cursor.comment() | system.profile 集合中进行跟踪。 |
cursor.count() | |
cursor.explain() | |
cursor.forEach() | |
cursor.hasNext() | true if the cursor has documents and can be iterated.true 。 |
cursor.hint() | |
cursor.isClosed() | true if the cursor is closed.true 。 |
cursor.isExhausted() | true if the cursor is closed and there are no objects remaining in the batch.true 。 |
cursor.itcount() | |
cursor.limit() | |
cursor.map() | |
cursor.max() | cursor.hint() cursor.hint() 配合使用 |
cursor.maxTimeMS() | |
cursor.min() | cursor.hint() cursor.hint() 配合使用 |
cursor.next() | |
cursor.noCursorTimeout() | |
cursor.objsLeftInBatch() | |
cursor.readConcern() | db.collection.find() operation.db.collection.find() 操作的读取关注点。 |
cursor.readPref() | |
cursor.returnKey() | |
cursor.showRecordId() | |
cursor.size() | skip() and limit() methods.skip() 和limit() 方法后游标中文档的计数。 |
cursor.skip() | |
cursor.sort() | |
cursor.tailable() | |
cursor.toArray() |
Database Methods数据库方法
db.aggregate() | |
db.createCollection() | |
db.createView() | |
db.commandHelp() | |
db.dropDatabase() | |
db.getCollection() | mongo shell. |
db.getCollectionInfos() | |
db.getCollectionNames() | |
db.getMongo() | |
db.getLogComponents() | |
db.getName() | |
db.getProfilingStatus() | slowOpThresholdMs 设置和slowOpSampleRate 设置。 |
db.getSiblingDB() | |
db.listCommands() | |
db.logout() | |
db.printShardingStatus() | |
db.runCommand() | |
db.setLogLevel() | |
db.setProfilingLevel() | slowms 和sampleRate 。 |
db.watch() |
Free Monitoring Methods免费监控方法
Starting in MongoDB 4.0, MongoDB (Community Edition) offers free cloud monitoring for standalone and replica set deployments.从MongoDB 4.0开始,MongoDB(Community Edition)为独立和副本集部署提供免费的云监控。
Free cloud monitoring is only available for MongoDB Community Edition.免费云监控仅适用于MongoDB社区版。
db.disableFreeMonitoring() | |
db.enableFreeMonitoring() | |
db.getFreeMonitoringStatus() |
Client-Side Field Level Encryption Methods客户端字段级加密方法
Limitations局限性
Automatic encryption is only available when只有当mongosh
is connected to an Atlas cluster or a MongoDB Enterprise Server.mongosh
连接到Atlas集群或MongoDB Enterprise Server时,自动加密才可用。For details, see Automatic Client-Side Field Level Encryption.有关详细信息,请参阅自动客户端字段级加密。The methods listed in this section are used for manual encryption, and are supported on non-enterprise servers.本节中列出的方法用于手动加密,并且在非企业服务器上受支持。Automatic encryption is not available with the Homebrew installation ofmongosh
.mongosh
的Homebrew安装不提供自动加密功能。Field level encryption is only available in the字段级加密仅在mongosh
binary, and not available in the embedded Compass shell.mongosh
二进制文件中可用,而在嵌入式Compass shell中不可用。
ClientEncryption.createEncryptedCollection() | |
ClientEncryption.decrypt() | encryptedValue if the current database connection was configured with access to the Key Management Service (KMS) and key vault used to encrypt encryptedValue .encryptedValue 的密钥保管库,则解密指定的encryptedValue 。 |
ClientEncryption.encrypt() | encryptionKeyId and encryptionAlgorithm .encryptionKeyId 和encryption_Algorithm 对指定的值进行加密。 |
getClientEncryption() | ClientEncryption object for the current database collection.ClientEncryption 对象。 |
getKeyVault() | KeyVault object for the current database connection.KeyVault 对象。 |
KeyVault.addKeyAlternateName() | keyAltName to the keyAltNames array of the data encryption key with the specified UUID.keyAltName 添加到具有指定UUID的数据加密密钥的keyAltNames 数组中。 |
KeyVault.createKey() | |
KeyVault.deleteKey() | |
KeyVault.getKey() | |
KeyVault.getKeyByAltName() | keyAltName .keyAltName 的所有数据加密密钥。 |
KeyVault.getKeys() | |
KeyVault.removeKeyAlternateName() | keyAltName from the data encryption key with the specified UUID. keyAltName 。 |
Native Methods原生方法
cd() | |
isInteractive() | mongosh 是以交互模式还是脚本模式运行。 |
load() | mongosh , scripts loaded with the load() method support the __filename and __dirname Node.js variables. mongosh 中,用load() 方法加载的脚本支持__filename 和__dirname Node.js变量。load() method.load() 方法不访问脚本的文件名或目录。 |
print() | print() and printjson() are aliases for console.log() .print() 和printjson() 是console.log() 的别名。
> print("hello world") |
pwd() | |
quit() | |
sleep() | mongo shell for a given period of time.mongo shell挂起一段给定的时间。 |
version() | mongosh instance.mongosh 实例的当前版本。 |
Query Plan Cache Methods查询计划缓存方法
db.collection.getPlanCache() | PlanCache methods for a collection.PlanCache 方法。 |
PlanCache.clear() | |
PlanCache.clearPlansByQuery() | |
PlanCache.help() | |
PlanCache.list() |
Replication Methods复制方法
rs.add() | |
rs.addArb() | |
rs.config() | |
rs.freeze() | mongosh is connected to ineligible to become primary for the specified duration. mongosh 连接到的副本集成员在指定的持续时间内没有资格成为主要成员。 |
db.getReplicationInfo() | |
rs.initiate() | |
db.printReplicationInfo() | mongosh is connected to.mongosh 连接到的副本集成员的操作日志。 |
rs.printReplicationInfo() | mongosh is connected to.mongosh 连接到的副本集成员的操作日志。 |
db.printSecondaryReplicationInfo | rs.printSecondaryReplicationInfo() method. rs.printSecondaryReplicationInfo() 方法相同。db.printSlaveReplicationInfo() method. db.printSlaveReplicationInfo() 方法。db.printSlaveReplicationInfo() method is deprecated in mongosh . mongosh 中不赞成使用db.printSlaveReplicationInfo() 方法。db.printSecondaryReplicationInfo() instead.db.printSecondaryReplicationInfo() 。rs.printSecondaryReplicationInfo() method issued on a replica set with two secondary members:rs.printSecondaryReplicationInfo() 方法的输出示例:
source: rs2.example.net:27017 |
rs.printSecondaryReplicationInfo | db.printSecondaryReplicationInfo() method. db.printSecondaryReplicationInfo() 方法相同。rs.printSlaveReplicationInfo() method in the legacy mongo shell. rs.printSlaveReplicationInfo() 方法。rs.printSlaveReplicationInfo() method is deprecated in mongosh . rs.printSlaveReplicationInfo() 方法在mongosh 中已被弃用。rs.printSecondaryReplicationInfo() instead.rs.printSecondaryReplicationInfo() 。rs.printSecondaryReplicationInfo() method issued on a replica set with two secondary members:rs.printSecondaryReplicationInfo() 方法的输出示例:
source: rs2.example.net:27017 |
rs.reconfig() | |
rs.remove() | |
rs.status() | mongosh is connected to.mongosh 连接到的副本集成员的状态。 |
rs.stepDown() | |
rs.syncFrom() | mongosh is connected to.mongosh 所连接的副本集成员的主机名指定的副本集。 |
Role Management Methods角色管理方法
db.createRole() | |
db.dropRole() | |
db.dropAllRoles() | |
db.getRole() | |
db.getRoles() | |
db.grantPrivilegesToRole() | |
db.revokePrivilegesFromRole() | |
db.grantRolesToRole() | |
db.revokeRolesFromRole() | |
db.updateRole() |
Session Object Methods会话对象方法
Mongo.startSession() | |
Session.advanceOperationTime() | |
Session.endSession() | |
Session.getClusterTime() | |
Session.getDatabase() | |
Session.getOperationTime() | |
Session.getOptions() | |
Session.hasEnded() | |
SessionOptions() | SessionOptions() object, use Session.getOptions() .SessionOptions() 对象,请使用Session.getOptions() 。 |
Server Status Methods服务器状态方法
db.hello() | mongod instance.mongod 实例的角色。mongod is a member of a replica set, then the isWritablePrimary and secondary fields report if the instance is the primary or if it is a secondary member of the replica set.mongod 是副本集的成员,则isWritablePrimary 和secondary 字段会报告该实例是副本集中的主要成员还是次要成员。 |
db.hostInfo() | |
db.collection.latencyStats() | |
db.printCollectionStats() | |
db.serverBuildInfo() | mongod instance.mongod 实例的编译参数。 |
db.serverCmdLineOpts() | |
db.serverStatus() | |
db.stats() | |
db.version() | mongod instance.mongod 实例的版本。 |
Sharding Methods分片方法
sh.addShard() | |
sh.addShardTag() | sh.addShardToZone() .sh.addShardToZone() 的别名。 |
sh.addShardToZone() | |
sh.addTagRange() | sh.updateZoneKeyRange() .sh.updateZoneKeyRange() 的别名。 |
sh.balancerCollectionStatus() | |
sh.disableAutoMerger() | |
sh.disableAutoSplit() | |
sh.disableBalancing() | |
sh.enableAutoMerger() | |
sh.enableAutoSplit() | |
sh.enableBalancing() | sh.disableBalancing() .sh.disableBalancing() 禁用了分片集合平衡器进程,则激活该进程。 |
sh.enableSharding() | |
sh.getBalancerState() | |
sh.isBalancerRunning() | |
sh.moveChunk() | |
sh.removeRangeFromZone() | |
sh.removeShardFromZone() | |
sh.removeShardTag() | |
sh.removeTagRange() | sh.addShardTag() method. sh.addShardTag() 方法创建的shard标记中。sh.removeRangeFromZone() in MongoDB 3.4.sh.removeRangeFromZone() 。 |
sh.setBalancerState() | |
sh.shardCollection() | |
sh.splitAt() | |
sh.splitFind() | |
sh.startAutoMerger() | |
sh.startBalancer() | |
sh.status() | |
sh.stopAutoMerger() | |
sh.stopBalancer() | |
sh.updateZoneKeyRange() |
Telemetry Methods遥测方法
These methods configure whether 这些方法配置mongosh
tracks anonymous telemetry data. mongosh
是否跟踪匿名遥测数据。Telemetry is enabled by default.默认情况下启用遥测。
For more information on what data 有关mongosh
tracks with telemetry, see Configure Telemetry Options.mongosh
使用遥测跟踪哪些数据的详细信息,请参阅配置遥测选项。
disableTelemetry() | mongosh .mongosh 的遥测。 |
enableTelemetry() | mongosh .mongosh 的遥测。 |
Transaction Methods事务处理方法
Session.abortTransaction() | |
Session.commitTransaction() | |
Session.startTransaction() |
User Management Methods用户管理方法
The passwordPrompt()
method is currently not supported in mongosh
. mongosh
当前不支持passwordPrompt()
方法。As a result, when using the following methods you must specify the password as a parameter:因此,使用以下方法时,必须将密码指定为参数:
db.auth() | |
db.changeUserPassword() | |
db.createUser() | |
db.dropAllUsers() | |
db.dropUser() | |
db.getUser() | |
db.getUsers() | |
db.updateUser() | |
db.grantRolesToUser() | |
db.revokeRolesFromUser() |