MongoDB创建和查询视图(一)

目录

限制和注意事项

应用两种方式创建视图


本文整理mongodb的官方文档,介绍mongodb的视图创建和查询。

Mongodb中,允许使用两种方式来创建视图。

//使用db.createCollection()来创建视图
db.createCollection(
  "<viewName>",
  {
    "viewOn": "<source>",
    "pipeline": [<pipeline>],
    "collation": {<collation>}
  }
)
//使用db.createView()来创建视图
db.createView(
  "<viewName>",
  "<source>",
  [<pipeline>],
  {
    "collation": { <collation>}
  }
)

限制和注意事项

  • 创建视图时,要创建的视图需要与依赖的集合在同一个数据库。
  • 在普通视图的定义的管道中中,不能出现$out和$merge过程。在$lookup或$facet过程中也不能出现$merge和$out。
  • 视图创建后,不可以被重新命名,不可以修改视图名称。
  • 视图中,不能够使用mapReduce(), $text, $geoNear等命令。
  • Mongodb查看集合的操作,如db.getCollectionInfos()和db.getCollectionNames()命令,结果集中会包含用户定义的视图信息。
  • 视图的定义对用户是可见的,使用命令db.getCollectionInfos()或通过explain查询执行计划时,打印出来的信息会包含视图的定义信息。因此,用户需要在定义视图中避免直接引用敏感字段和字段值。
  • 使用AtlasUI,用户只能创建物化视图。
  • 使用find()命令查询视图中,不支持操作符$, $elemMatch, $slice, $meta
  • 使用db.collection.find()方法查询视图时,filter, projection, sort, skip, limit等查询方法,转化成等价的集合管道查询方法。
  • Mongodb将客户端视图查询条件与视图定义中的管道操作一起进行查询优化
  • 查询优化器不会改变视图的查询结果,只是重新编排管道中查询操作的顺序来提高效率。
  • 使用db.createView()命令创建视图的过程中,会在依赖的集合上加锁。所有对该集合的操作需要等视图创建结束后才能执行。
  • 创建视图的过程中,Mongodb会在system.view集合上加锁,当创建视图结束后,该锁才会被释放。

应用两种方式创建视图

构建一个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
}

猜你喜欢

转载自blog.csdn.net/wilsonzane/article/details/135203210