3.4-数据库命令
Create by fall on 01 Apr 2025
Recently revised in 01 Apr 2025
数据库命令
视图
创建视图
db.createCollection()
db.createView()
必须在与源集合相同的数据库中创建视图。视图一旦创建便无法重命名。
// 创建集合的方式
db.createCollection(
"<viewName>", {
"viewOn" : "<source>",
"pipeline" : [<pipeline>],
"collation" : { <collation> }
})
db.createView( "<viewName>", "<source>",
[<pipeline>],
{
"collation" : { <collation> }
}
)
db.createView(
"firstYears", // 视图名称
"users", // 所依据的集合
[ { $match: { year: 1 } } ] // 匹配的信息
)
查询视图
- 视图名称包含在集合列表输出中
db.getCollectionInfos()
和db.getCollectionNames()
// 对集合 graduateStudents 排序
db.graduateStudents.aggregate(
[
{ $sort: { name: 1 } },
{ $unset: [ "_id" ] }
]
)
用户
// 创建角色
db.createRole({
role: "Billing",
privileges: [
{
resource: { db: "test", collection: "medicalView" },
actions: [ "find" ]
}
],
roles: [ ] })
// 创建用户
db.createUser({
user: "James",
pwd: "js008",
roles: [
{ role: "Billing", db: "test" }
]
})