The following document lists the available methods in the MongoDB Shell. Click a method to see its documentation in the MongoDB Manual, including syntax and examples.以下文档列出了MongoDB Shell中的可用方法。单击方法查看MongoDB手册中的文档,包括语法和示例。
Important
Methods which reset the shell's connection to the server trigger a termination of all open server sessions. When a session ends, all in-progress operations are terminated as well, and the client must re-authenticate if authentication is enabled.重置shell与服务器的连接的方法会触发所有打开的服务器会话的终止。当会话结束时,所有正在进行的操作也会终止,如果启用了身份验证,客户端必须重新进行身份验证。
The following methods reset the shell's connection:以下方法重置shell的连接:
db.authMongo.setReadPrefMongo.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搜索索引允许您在Atlas搜索中查询数据。Atlas Search indexes enable performant text search queries by mapping search terms to the documents that contain those terms.Atlas搜索索引通过将搜索词映射到包含这些词的文档来实现高性能的文本搜索查询。
Use the following methods to manage Atlas Search indexes.使用以下方法管理Atlas搜索索引。
Important
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. The builder constructs an unordered list of write operations that MongoDB executes in bulk.Bulk()操作生成器。构建器构建了一个MongoDB批量执行的无序写操作列表。 |
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() | hint(提示)选项,该选项指定支持批量操作的索引。 |
Bulk.find.remove() | |
Bulk.find.removeOne() | |
Bulk.find.replaceOne() | |
Bulk.find.updateOne() | |
Bulk.find.update() | multi更新操作添加到批量操作列表中。该方法更新现有文档中的特定字段。 |
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. Use db.collection.countDocuments() or db.collection.estimatedDocumentCount() instead.mongosh 1.0.6中已弃用。请改用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.size字段包裹在collStats的输出中。 |
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.getDB() | ||||||||||
Mongo.setReadPref() | config读取首选项。 | |||||||||
Mongo.watch() | admin, local, and config databases.admin、local和config数据库除外。 |
Cursor Methods游标方法
cursor.addOption() | |
cursor.batchSize() |
|
cursor.close() | |
cursor.collation() | db.collection.find().db.collection.find()返回的游标的排序规则。 |
cursor.comment() | |
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() | |
db.getSiblingDB() | |
db.listCommands() | |
db.logout() | |
db.printShardingStatus() | |
db.runCommand() | |
db.setLogLevel() | |
db.setProfilingLevel() | |
db.watch() |
In-Use Encryption Methods正在使用的加密方法
Note
Limitations局限性
Automatic encryption is only available when自动加密仅在mongoshis connected to an Atlas cluster or a MongoDB Enterprise Server.mongosh连接到Atlas集群或MongoDB Enterprise Server时可用。For details, see Automatic 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字段级加密仅在mongoshbinary, 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和encryptionAlgorithm对指定值进行加密。 |
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本机方法
|
|
|
|
| mongosh是在交互模式还是脚本模式下运行。 |
|
|
|
|
|
|
|
| |
|
| mongo shell for a given period of time. |
|
| 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. You must specify the duration in seconds.mongosh连接到的副本集成员在指定的持续时间内没有资格成为主要成员。您必须以秒为单位指定持续时间。 |
db.getReplicationInfo() | |
rs.initiate() | |
db.printReplicationInfo() | mongosh is connected to.mongosh连接到的副本集成员的oplog。 |
rs.printReplicationInfo() | mongosh is connected to.mongosh连接到的副本集成员的oplog。 |
db.printSecondaryReplicationInfo |
|
rs.printSecondaryReplicationInfo |
|
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() |
|
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分片方法
db.collection.getShardLocation() | |
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.disableMigrations() | setAllowMigrations command.setAllowMigrations命令。 |
sh.enableAutoMerger() |
|
sh.enableAutoSplit() | |
sh.enableBalancing() | sh.disableBalancing().sh.disableBalancing()禁用了分片集合平衡器进程,则激活该进程。 |
sh.enableMigrations() | sh.disableMigrations(). sh.disableMigrations()禁用的分片群集中的特定集合启用块迁移。setAllowMigrations command.setAllowMigrations命令。 |
sh.enableSharding() | |
sh.getBalancerState() | |
sh.getShardedDataDistribution() | sh.getShardedDataDistribution()$shardedDataDistribution aggregation pipeline stage.$shardedDataDistribution聚合管道阶段的shell辅助方法。 |
sh.isBalancerRunning() | |
sh.isConfigShardEnabled() | sh.isConfigShardEnabled() also returns host and tag information.sh.isConfigShardEnabled()也会返回主机和标签信息。 |
sh.listShards() | |
sh.moveChunk() | |
sh.removeRangeFromZone() | |
sh.removeShardFromZone() | |
sh.removeShardTag() | |
sh.removeTagRange() | sh.addShardTag() method. sh.addShardTag()方法创建的分片标记中。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. Telemetry is enabled by default.mongosh是否跟踪匿名遥测数据。默认情况下启用遥测。
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用户管理方法
Important
The passwordPrompt() method is currently not supported in mongosh. As a result, when using the following methods you must specify the password as a parameter:mongosh当前不支持passwordPrompt()方法。因此,使用以下方法时,必须将密码指定为参数:
db.auth() | |
db.changeUserPassword() | |
db.createUser() | |
db.dropAllUsers() | |
db.dropUser() | |
db.getUser() | |
db.getUsers() | |
db.updateUser() | |
db.grantRolesToUser() | |
db.revokeRolesFromUser() |