码迷,mamicode.com
首页 > 其他好文 > 详细

mongo

时间:2019-04-02 10:40:59      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:mon   两种   关联   mongo   match   weight   update   哪些   删除   

mongo中n对n的关联关系的基本操作

学生表

db.students.remove({})
db.students.insert({_id:"1",name : "a"})
db.students.insert({_id:"2",name : "b"})
db.students.insert({_id:"3",name : "c"})

 

课程表

db.class.remove({})
db.class.insert({_id:"4",classname:"english"})
db.class.insert({_id:"5",classname:"math"})
db.class.insert({_id:"6",classname:"chinese"})

 


学生选课表:两种表现形式

db.choose.remove({})
db.choose.insert({_id:"1",name:"a",class:[{_id:"5",classname:"math"},{_id:"6",classname:"chinese"}]})
db.choose.insert({_id:"1",name:"a",class:[{_id:"4",classname:"english"},{_id:"6",classname:"chinese"}]})
db.choose.insert({_id:"1",name:"a",class:[{_id:"4",classname:"english"},{_id:"5",classname:"math"}]})

 

db.choose.remove({})
db.choose.insert({_id:"1",name:"a",class:["5","6"]})
db.choose.insert({_id:"1",name:"a",class:["4","6"]})
db.choose.insert({_id:"1",name:"a",class:["4","5"]})

 

1. 查看a同学所选的课程

db.choose.find({_id:"1"},{class: 1._id: 0})

 

2.查看english课有哪些同学选

db.choose.find(
{class: {$eleMatch: {classname: "english"}}},
{_id,1}
)

 

db.choose.find({class:"5"},{_id:1,name:1})

 

3.删除a同学

db.choose.remove({name:"a"})

 

4.删除english课

db.choose.update({},{"$pull":{class:{classname:"english"}}}, {multi: true})

db.choose.update({},{"$pull":{class:"4"}},{multi:true})

 

5.a同学增加english

db.choose.update({name:"a"},{"$push":{class:{_id:"4",classname:"english"}}})

db.choose.update({name:"a"},{"$push":{class:"4"}})

 

mongo

标签:mon   两种   关联   mongo   match   weight   update   哪些   删除   

原文地址:https://www.cnblogs.com/dadddd/p/10640939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!