MongoDB教程
MongoDB运算符
MongoDB命令
MongoDB数据库
MongoDB Shell
MongoDB云和工具

MongoDB 数据库命令

MongoDB数据库命令用于创建,修改和更新数据库。

#1、 db.adminCommand(cmd)

通过提供帮助程序,admin命令方法针对admin数据库运行以运行指定的数据库命令。
命令: 要么以文档形式指定参数或字符串形式。如果命令定义为字符串,则它不能包含任何参数。
示例:
在admin上创建一个具有dbOwner角色的lidihuo用户数据库。
db.adminCommand(
  {
    createUser: "lidihuo",
    pwd: passwordPrompt(),
    roles: [
      { role: "dbOwner", db: "admin" }
    ]
  }
)
输出:
MongoDB数据库命令

#2、 db.aggregate()

聚合方法初始化一个特定的诊断或管理管道,该管道不需要anu基础集合。
语法:
db.aggregate( [ <pipeline> ], { <options> } )
pipeline参数不需要任何基础集合,并且始终从兼容阶段开始,例如$currentOp或$listLocalSessions。它是将执行的一系列阶段。
示例:
以下示例运行具有两个阶段的管道。第一个是$currentOp操作,第二个将过滤结果。
use admin
db.aggregate( [ {
   $currentOp : { allUsers: true, idleConnections: true } }, 
{
   $match : { shard: "shardDemo" }
   }
] )
输出:
MongoDB数据库命令

#3、 db.cloneDatabase("hostname")

clonedatabase方法将指定的数据库复制到当前数据库,并假定远程位置的数据库与当前数据库具有相同的名称。
hostname参数包含我们要复制的数据库的hostname。
示例:
db.cloneDatabase("customers")
输出:
MongoDB数据库命令

#4、 db.commandHelp(command)

我们使用commandHelp方法为指定的数据库命令提供了帮助选项。命令参数包含数据库命令的名称。
MongoDB数据库命令

#5、 db.createCollection(name,options)

将使用此方法创建新的集合或视图。 createCollection方法主要用于在命令中首次引用该集合时创建使用特定选项的新集合。
例如-我们将创建一个lidihuo 集合和 JSON 模式验证器:
db.createCollection( "student", {
   validator: { $jsonSchema: {
      bsonType: "object",
      required: [ "phone" ],
      properties: {
         phone: {
            bsonType: "string",
            description: "must be a string and is required"
         },
         email: {
            bsonType : "string",
            pattern: "@mongodb\.com$",
            description: "must be a string and match the regular expression pattern"
         },
         status: {
            enum: [ "Unknown", "Incomplete" ],
            description: "can only be one of the enum values"
         }
      }
   } }
} )

MongoDB数据库命令

#6、 db.createView()

将指定的聚合管道应用于集合时,createView方法将为集合创建一个新视图。该方法可以在读取操作期间进行计算,并用作只读操作。可以在源集合的同一数据库中创建视图,以作为基础聚合管道的一部分执行读取操作。
语法:
db.createView(<view>, <source>, <pipeline>, <options>)
以下示例使用_id,student.management和Department字段创建StudentFeedback视图:
db.createView(
   "StudentFeedback",
   "survey",
   [ { $project: { "management": "$Student.management", department: 1 } } ]
)
输出:
MongoDB数据库命令

#7、 db.dropDatabase( )

drop方法将删除指定的数据库和关联的数据文件。
例如-
<我们使用 操作将当前数据库切换到临时数据库。我们使用db.dropDatabase()方法删除临时数据库
use temp
db.dropDatabase()

#8、 db.getLogComponents()

getLog方法返回当前的设置。该方法确定由 MongoDB 为每个日志消息组件生成的日志消息的数量。
示例:
{
   "verbosity" : 0,
   "accessControl" : {
      "verbosity" : -1
   },
   "command" : {
      "verbosity" : -1
   },
   "control" : {
      "verbosity" : -1
   },
   "geo" : {
      "verbosity" : -1
   },
   "index" : {
      "verbosity" : -1
   },
   "network" : {
      "verbosity" : -1
   },
   "query" : {
      "verbosity" : 2
   },
   "replication" : {
      "verbosity" : -1,
      "election" : {
         "verbosity" : -1
      },
      "heartbeats" : {
         "verbosity" : -1
      },
      "initialSync" : {
         "verbosity" : -1
      },
      "rollback" : {
         "verbosity" : -1
      }
   },
   "sharding" : {
      "verbosity" : -1
   },
   "storage" : {
      "verbosity" : 2,
      "recovery" : {
         "verbosity" : -1
      },
      "journal" : {
         "verbosity" : -1
      }
   },
   "write" : {
      "verbosity" : -1
   }
}

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4