mongo
Posted dadddd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章