MongoDB数据库命令用于创建,修改和更新数据库。
db.adminCommand(cmd)
admin命令方法针对admin数据库运行,以通过提供帮助程序来运行指定的数据库命令。
命令:参数以文档形式或字符串形式指定。如果命令定义为字符串,则它不能包含任何参数。
示例:
在管理数据库上创建一个具有dbOwner角色的名为Learnfk的用户。
db.adminCommand(
{
createUser: "Learnfk",
pwd: passwordPrompt(),
roles: [
{ role: "dbOwner", db: "admin" }
]
}
)
db.aggregate()
聚合方法初始化一个特定的诊断或管理管道。
语法:
db.aggregate( [ <pipeline> ], { <options> } )
Pipeline参数不需要任何基础集合,并且始终以兼容的阶段开始,例如$currentOp或$listLocalSessions。它是将执行的一系列阶段。
示例:
以下示例运行具有两个阶段的管道。第一个是$currentOp操作,第二个将过滤结果。
use admin
db.aggregate( [ {
$currentOp : { allUsers: true, idleConnections: true } },
{
$match : { shard: "shardDemo" }
}
] )
输出:

db.clonedatabase("hostname")
clonedatabase方法会将指定的数据库复制到当前数据库,并假定远程位置的数据库与当前数据库具有相同的名称。
hostname参数包含无涯教程要复制的数据库的主机名。
示例:
db.cloneDatabase("Customers")
输出:

db.commandHelp(cmd)
无涯教程使用commandHelp方法为指定的数据库命令提供了帮助选项。命令参数包含数据库命令的名称。

db.createCollection(name,options)
使用此方法将创建一个新的集合或视图。 createCollection方法主要用于在命令中首次引用该集合时创建使用特定选项的新集合。
例如 - 无涯教程将创建一个learnfk集合与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"
}
}
} }
} )

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

db.dropDatabase(<writeConcern>)
drop方法删除指定的数据库和关联的数据文件。
例如-
无涯教程使用<database>操作将当前数据库切换到临时数据库。无涯教程使用db.dropdatabase()方法来删除临时数据库
use temp db.dropDatabase()
db.getLogComponents()
GetLog方法返回当前级别的设置。
示例:
{
"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
}
}