目录
本文整理mongodb的官方文档,介绍mongodb的视图创建和查询。
Mongodb中,允许使用两种方式来创建视图。
//使用db.createCollection()来创建视图
db.createCollection(
"<viewName>",
{
"viewOn": "<source>",
"pipeline": [<pipeline>],
"collation": {<collation>}
}
)
//使用db.createView()来创建视图
db.createView(
"<viewName>",
"<source>",
[<pipeline>],
{
"collation": { <collation>}
}
)
构建一个student集合,用于创建视图
db.students.insertMany( [
{ sID: 22001, name: "Alex", year: 1, score: 4.0},
{ sID: 21001, name: "bernie", year: 2, score: 3.7},
{ sID: 20010, name: "Chris", year: 3, score: 2.5},
{ sID: 22021, name: "Drew", year: 1, score: 3.2},
{ sID: 17301, name: "harley", year: 6, score: 3.1},
{ sID: 21022, name: "Farmer", year: 1, score: 2.2},
{ sID: 20020, name: "george", year: 3, score: 2.8},
{ sID: 18020, name: "Harley", year: 5, score: 2.8}
])
使用db.createView()创建一个视图,查询出一年级学生的数据
db.createView(
"V_firstYears", //视图名称
"students", //基于集合students创建视图
[{ $match: {year: 1}}] //集合查询,匹配students表中一年级学生的数据
)
查询集合V_firstYears的数据
db.V_firstYears.find({},{_id: 0}) //使用{_id: 0}关闭返回结果中文档id
/* 1 createdAt:12/25/2023, 4:01:19 PM*/
{
"_id" : ObjectId("658936cfe0ac6d3d14d04bde"),
"sID" : 22001,
"name" : "Alex",
"year" : 1,
"score" : 4
},
/* 2 createdAt:12/25/2023, 4:01:19 PM*/
{
"_id" : ObjectId("658936cfe0ac6d3d14d04be1"),
"sID" : 22021,
"name" : "Drew",
"year" : 1,
"score" : 3.2
},
/* 3 createdAt:12/25/2023, 4:01:19 PM*/
{
"_id" : ObjectId("658936cfe0ac6d3d14d04be3"),
"sID" : 21022,
"name" : "Farmer",
"year" : 1,
"score" : 2.2
}
使用db.createCollection()方法创建一个查询毕业生的视图。
db.createCollection(
"v_graduateStudents",
{
viewOn: "students",
pipeline: [ { $match: { $expr: { $gt: ["$year", 4]}}}],//查询超过4年的数据
collation: { locale: "en", caseFirst: "upper"} //添加字符序定义,指定排序方法
}
)
查询定义的毕业生视图。查询过程中,添加了按照学生姓名进行排序。定义视图时指定了按照大写字母优先的排序规则,则Harey排在前面。
db.v_graduateStudents.find({},{_id: 0}).sort('name')
/* 1 */
{
"sID" : 18020,
"name" : "Harley",
"year" : 5,
"score" : 2.8
},
/* 2 */
{
"sID" : 17301,
"name" : "harley",
"year" : 6,
"score" : 3.1
}
删除视图,重新建立一个小写字母优先的字符序规则
db.v_graduateStudents.drop()
db.createCollection(
"v_graduateStudents",
{
viewOn: "students",
pipeline: [ { $match: { $expr: { $gt: ["$year", 4]}}}],
collation: { locale: "en", caseFirst: "lower"}
}
)
查询新建的视图,返回结果与前面的排序结果不同。
db.v_graduateStudents.find({},{_id: 0}).sort('name')
/* 1 */
{
"sID" : 17301,
"name" : "harley",
"year" : 6,
"score" : 3.1
},
/* 2 */
{
"sID" : 18020,
"name" : "Harley",
"year" : 5,
"score" : 2.8
}