开发者学堂课程【场景实践 - 基于MongoDB实现商品管理系统:Mongodb的多表查询讲解】学习笔记与课程紧密联系,让用户快速学习知识
课程地址:https://developer.aliyun.com/learning/course/728/detail/13002
Mongodb的多表查询讲解
内容介绍:
一、类似于关系型数据库中的where查询
二、多表查询
一、类似于关系型数据库中的 where 查询
需求:查询 age 是23的人的信息
代码如下所示:
先查询所有信息,再查询age为23的人的信息。
db.persons.find()
{"_id”: ObjectId("5aa8debebaf95f1915073eaf"),"name" : " zhangsan","age”:23,"email":"17161717@qqom","c”:89,”m”: 78,"e" : 99,"country" : "china", "books": [“Js",“JAVA","MKONCODB”]}
{"_id":0bjectId("5aa8debebaf95f1915073eb0"),"nane”:"linchong"," age" : 20,"emai1":"234681717@qq.com" "c”:82,“m”:38,“e”:69,“country”:“china”,“books”:[“JS”,“PHP”,“MNGDB”]}
{"_id" : ObjectId("5aa8debebaf95f1915073eb1"),"name": "1uzhishen",“age: 23,"email”:"171615151@qq.com"c": 23,“m”:98,"e":100,"country": "china","books":[“Jquery"' “"JAVA",“ MONGODB" ] }
{"_id” : Objectid("5a8debebaf9511915073eb2"),"name": " songjiang", " age" : 33, " enaj1":"1781918717@qq.com","c" :89,“m”: 78,"e" : 99," country": " china", "books" : [ "mysql","JAVA",“MIONGODB"]}
{"_id" : ObjectId("5aa8debebaf95f1915073eb3"), " mnane": "wusong , " age”: 28, " email”:“"123456@qq.com " c": 76, "h”: 32,"e” : 99," country" :"Korea","books" : [ “C++","JAVA",“C"]}
{"_id":ObjectId("5aa8debebaf95f1915073eb4"),"name": likui","age" : 43, "email”:"234567@qq.con" "c”:23,"m":46,“e”:10,“country”:“USA”,“books”:[“jsp”,“JAVA”,“MNGDB”]}
{"_id" : ObjectId("5aa8debebaf95f1915073eb5*7),,"nane" : " shigian","age": 23," email" :"8171717@qq.com","c": 19, "m" : 88,“e” : 99,"country": "china",“books" : [ "mysql","oracle",“MONGODB"]}
{"_id" : 0biectId("5aa8debebaf9511915073eb6"),"name" : "wuyong", " age":32, " email”:"17827277@qq.com , " "c”: 76,"m":88,"e”:99,"country":"USA",“books" : [ “C++","hibernate",“C”]}
db. persons.find( {"age":23})
{"_id”: ObjectId("5aa8debebaf95f1915073eaf"),"name" : " zhangsan","age”:23,"email":"17161717@qqom","c”:89,”m”: 78,"e" : 99,"country" : "china", "books": [“Js",“JAVA","MKONCODB”]}
{"_id" : ObjectId("5aa8debebaf95f1915073eb1"),"name": "1uzhishen",“age: 23,"email”:"171615151@qq.com"c": 23,“m”:98,"e":100,"country": "china","books":[“Jquery"' “"JAVA",“ MONGODB" ] }
{"_id" : ObjectId("5aa8debebaf95f1915073eb5*7),,"nane" : " shigian","age": 23," email" :"8171717@qq.com","c": 19, "m" : 88,“e” : 99,"country": "china",“books" : [ "mysql","oracle",“MONGODB"]}
说明:db. persons.find( {"age":23})
,在MySQL数据库中相当于select * from persons where age=23
需求二:查询age是23,并且 name 是 songjiang 的人的 name,age,country.
如果不想查询 id,输入_id=0
db.persons.find( {"age" : 33,"name" :" songjiang""),
{
name : 1, age: 1, country:1,_id:0} )
{"name" : "songjiang", "age" : 33,"country" : "china"}
二、多表查询
在 mongodb 中,多表查询可以有两种实现方案:嵌套和引用。比较常用的是嵌套。
1.嵌套
假设班级和学生之间的关系,属于一对多的关系。一个班级对应多个学生。
同时增加:
代码:var classes={id:1,name:"java",students:[[sid:1,name:"zhangsan"),
{sid:2,name:"lisi"}]};
db.classes.insert(classes);
代码如下:
var classes={id:1,name:"java",students:[[sid:1,name:"zhangsan"),
{sid:2,name:"lisi"}]};
db.classes.insert(classes);
db.classes.find()
{"_id" : ObjectId("5aa93338baf951915073eb7")," id”: 1,"name” : "java" " students": [ {"sid”: 1 "name": "zhangsan"],{ "sid" : 2,"name":"1isi" } ]}
可以看到,在 classes 中有一个 key 值为 students,而这个 students 描述了多个学生,所以是数组。这里 classes 与 students 就是 sql 中的一对多的关系。
需求一:查询班级是java的班级中第一个学生的id。
代码:
db.classes.find({"name":"java"}).forEach(function(e){printjson(e.students[0].sid)});
演示:{"_id" : ObjectId("5aa93338baf951915073eb7")," id”: 1,"name” : "java" " students": [ {"sid”: 1 "name": "zhangsan"],{ "sid" : 2,"name":"1isi" } ]}
db.classes.find({"name":"java"}).forEach(function(e){printjson(e.students[0].sid)});
1
出现1,说明需求一已经完成。
需求二:查询id为1的班级中的所有的学生
第一种方法如下:
db.classes.find( "id":1 , {"students":1, "_id":0})
{"students": [{ "sid”: 1,"name" :"zhangsan"), { "sid”: 2,"name" :"lisi" }]}
第二种方法:
db.classes.findOne().students
[
"sid”: 1,
"name" :"zhangsan"
},
{
"sid”: 2,
"name" :"lisi"
}
]