mongodb3.4创建只读视图实例

mongodb3.4创建只读视图实例

Aji_King
这是一个MongoDB3.4创建视图的实例
 


准备工作


* 首先创建一个名为test的数据库
* 在test数据库中创建两个集合,分别为users和groups
* 分别为两个数据集合录入若干数据:如下面db.users.find()和db.groups.find()查询结果所示
*
* 本实例分别演示了单集合视图多集合视图的创建和查询的过程

*/


linux下输入mongo进入mongodb的命令行模式

[root@~]# mongo

 

//进入test库

> use test

 

/**
* 查看所有数据集
*/
> show collections
groups
users


//查看用户表中数据(预设)
> db.users.find() 
{ "_id" : ObjectId("5852428b50df188fee654180"), "username" : "php_man", "sex" : 1, "age" : 33, "group_id" : ObjectId("5852401c50df188fee654176") }
{ "_id" : ObjectId("585242ab50df188fee654182"), "username" : "java_girl", "sex" : 2, "age" : 27, "group_id" : ObjectId("5852401c50df188fee654176") }
{ "_id" : ObjectId("5852436150df188fee654184"), "username" : "js_man", "sex" : 1, "age" : 28, "group_id" : ObjectId("5852405d50df188fee654178") }
{ "_id" : ObjectId("5852439e50df188fee654186"), "username" : "css_girl", "sex" : 2, "age" : 26, "group_id" : ObjectId("5852405d50df188fee654178") }

//查看用户组表中数据(预设)
> db.groups.find() 
{ "_id" : ObjectId("5852401c50df188fee654176"), "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180") }

{ "_id" : ObjectId("5852405d50df188fee654178"), "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184") }

 

创建单集合视图 

db.createView(视图名称,集合名称,条件)

> db.createView("man","users",{$match:{sex:1}})
{ "ok" : 1 }

// 查询视图,和集合的方式完全一样
> db.man.find()
{ "_id" : ObjectId("5852428b50df188fee654180"), "username" : "php_man", "sex" : 1, "age" : 33, "type" : "php" }
{ "_id" : ObjectId("5852436150df188fee654184"), "username" : "js_man", "sex" : 1, "age" : 28, "type" : "h5" }

 

创建多集合视图一

视图说明:查询用户组信息 时 关联查询用户信息

createView("视图名称","主查询集合",[{$lookup:{from:"关联集合",localField:"关联字段",foreignField:"外键",as:"为关联集合命名"}},{$project:{查询结果显示条件}}])


> db.createView("group_user","groups",[{$lookup:{from:"users",localField:"zuzhang",foreignField:"_id",as:"zuzhang_info"}},{$project:{"zuzhang_info._id":0}}])
{ "ok" : 1 }

> db.group_user.find()
{ "_id" : ObjectId("5852401c50df188fee654176"), "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180"), "zuzhang_info" : [ { "username" : "php_man", "sex" : 1, "age" : 33, "type" : "php" } ] }
{ "_id" : ObjectId("5852405d50df188fee654178"), "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184"), "zuzhang_info" : [ { "username" : "js_man", "sex" : 1, "age" : 28, "type" : "h5" } ] }

创建多集合视图二

视图说明:查询用户信息 时 关联查询用户组信息 


createView("视图名称","主查询集合",[{$lookup:{from:"关联集合",localField:"关联字段",foreignField:"外键",as:"为关联集合命名"}},{$project:{查询结果显示条件}}])

> db.createView("user_group","users",[{$lookup:{from:"groups",localField:"group_id",foreignField:"_id",as:"group_info"}},{$project:{"group_info._id":0}}])
{ "ok" : 1 }


> db.user_group.find()
{ "_id" : ObjectId("5852428b50df188fee654180"), "username" : "php_man", "sex" : 1, "age" : 33, "group_id" : ObjectId("5852401c50df188fee654176"), "group_info" : [ { "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180") } ] }
{ "_id" : ObjectId("585242ab50df188fee654182"), "username" : "java_girl", "sex" : 2, "age" : 27, "group_id" : ObjectId("5852401c50df188fee654176"), "group_info" : [ { "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180") } ] }
{ "_id" : ObjectId("5852436150df188fee654184"), "username" : "js_man", "sex" : 1, "age" : 28, "group_id" : ObjectId("5852405d50df188fee654178"), "group_info" : [ { "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184") } ] }
{ "_id" : ObjectId("5852439e50df188fee654186"), "username" : "css_girl", "sex" : 2, "age" : 26, "group_id" : ObjectId("5852405d50df188fee654178"), "group_info" : [ { "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184") } ] }

 

创建多集合视图三

视图说明:查询用户信息 时 关联查询用户组信息 并将 用户组的组长信息关联查询出来 
* 该视图比较特殊的地方是将两个已存在的视图关联起来创建成为一个新的视图

@createView("视图名称","主查询集合",[{$lookup:{from:"关联集合",localField:"关联字段",foreignField:"外键",as:"为关联集合命名"}},{$project:{查询结果显示条件}}])


> db.createView("user_group_zuzhang","user_group",[{$lookup:{from:"group_user",localField:"group_id",foreignField:"_id",as:"group_info"}},{$project:{"user_group.group_info":0}}])
{ "ok" : 1 }


> db.user_group_zuzhang.find()
{ "_id" : ObjectId("5852428b50df188fee654180"), "username" : "php_man", "sex" : 1, "age" : 33, "group_id" : ObjectId("5852401c50df188fee654176"), "group_info" : [ { "_id" : ObjectId("5852401c50df188fee654176"), "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180"), "zuzhang_info" : [ { "username" : "php_man", "sex" : 1, "age" : 33, "group_id" : ObjectId("5852401c50df188fee654176") } ] } ] }
{ "_id" : ObjectId("585242ab50df188fee654182"), "username" : "java_girl", "sex" : 2, "age" : 27, "group_id" : ObjectId("5852401c50df188fee654176"), "group_info" : [ { "_id" : ObjectId("5852401c50df188fee654176"), "groupname" : "后端组", "zuzhang" : ObjectId("5852428b50df188fee654180"), "zuzhang_info" : [ { "username" : "php_man", "sex" : 1, "age" : 33, "group_id" : ObjectId("5852401c50df188fee654176") } ] } ] }
{ "_id" : ObjectId("5852436150df188fee654184"), "username" : "js_man", "sex" : 1, "age" : 28, "group_id" : ObjectId("5852405d50df188fee654178"), "group_info" : [ { "_id" : ObjectId("5852405d50df188fee654178"), "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184"), "zuzhang_info" : [ { "username" : "js_man", "sex" : 1, "age" : 28, "group_id" : ObjectId("5852405d50df188fee654178") } ] } ] }
{ "_id" : ObjectId("5852439e50df188fee654186"), "username" : "css_girl", "sex" : 2, "age" : 26, "group_id" : ObjectId("5852405d50df188fee654178"), "group_info" : [ { "_id" : ObjectId("5852405d50df188fee654178"), "groupname" : "前端组", "zuzhang" : ObjectId("5852436150df188fee654184"), "zuzhang_info" : [ { "username" : "js_man", "sex" : 1, "age" : 28, "group_id" : ObjectId("5852405d50df188fee654178") } ] } ] }

 

// 查看所有数据集 

> show collections
group_user
groups
man
system.views
user_group
user_group_zuzhang
users

 

查看创建的视图

视图中并不存放数据,而是将视图的相关信息及关联数据集的规则放到system.views中
查询视图的时候实际上是通过查询多个数据集,然后将结果汇集到聚合管道(Aggregation Pipeline),最后将数据组装到一起再返回



> db.system.views.find()
{ "_id" : "test.man", "viewOn" : "users", "pipeline" : [ { "$match" : { "sex" : 1 } } ] }
{ "_id" : "test.group_user", "viewOn" : "groups", "pipeline" : [ { "$lookup" : { "from" : "users", "localField" : "zuzhang", "foreignField" : "_id", "as" : "zuzhang_info" } }, { "$project" : { "zuzhang_info._id" : 0 } } ] }
{ "_id" : "test.user_group", "viewOn" : "users", "pipeline" : [ { "$lookup" : { "from" : "groups", "localField" : "group_id", "foreignField" : "_id", "as" : "group_info" } }, { "$project" : { "group_info._id" : 0 } } ] }
{ "_id" : "test.user_group_zuzhang", "viewOn" : "user_group", "pipeline" : [ { "$lookup" : { "from" : "group_user", "localField" : "group_id", "foreignField" : "_id", "as" : "group_info" } }, { "$project" : { "user_group.group_info" : 0 } } ] }

posted @ 2019-08-22 09:17  grj001  阅读(524)  评论(0编辑  收藏  举报