文档间的关系
一、One to One
例:夫妻
db.WifeAndHusband.insert([
{
name:"黄蓉",husband:{name:"郭靖"}
},
{
name:"潘金莲",husband:{name:"武大郎"}
},
])
db.WifeAndHusband.find()
二、One to Many(Many to One)
例: 用户-订单 文章-评论
db.users.insert([
{username:"swk"},{username:"zbj"}
])
db.order.insert({
list:["漫画","肉"],
user_id:ObjectId("5d48ef25c57dfb3261f4086c")
})
db.order.find()
查找用户swk订单, 修改username 即可查询不同订单
var user_id = db.users.findOne({username:"zbj"})._id
user_id
db.order.find({user_id:user_id})
三、Many to Many 即user_id字段变数组
例:分类-商品,老师-学生*/
db.students.find()
db.teachers.insert([
{name:"洪七公"},
{name:"黄药师"},
{name:"龟仙人"}
]);
db.students.insert([
{name:"郭靖",
teach_ids:[ObjectId("5d48f250c57dfb3261f40870"),ObjectId("5d48f250c57dfb3261f40871")]
}
])
db.students.insert([
{name:"孙悟空",
teach_ids:[ObjectId("5d48f250c57dfb3261f40870"),ObjectId("5d48f250c57dfb3261f40871"),ObjectId("5d48f250c57dfb3261f40872")]
}
])
*待解决:多对多查询方法