MongoDB文档之间的关系

一对一(one to one)
一对多(one to many)
多对多(many to many)

一对一

db.studentAndTeacher.insert({
    name: 'Ferguson',
    disciple: {
        name: 'Cristiano'
    }
})

一对多

db.users.insert([
    {username: 'YFX'},
    {username: 'JCZ'}
])

db.order.insert({
    list: ["banana", "milk", "yogurt"],
    userid: ObjectId("5e58de3f436ce823fb4b49da") //id of 'YFX'
})

多对多

db.teachers.insert([
    {name: "Tony"},
    {name: "Luka"},
    {name: "Casai"}
])

db.students.insert([
    {
        name: "Carim",
        teacher_id: [ObjectId("5e58e0dc436ce823fb4b49dd"), ObjectId("5e58e0dc436ce823fb4b49de")]
    },
    {
    name: "Marcelo",
        teacher_id: [ObjectId("5e58e0dc436ce823fb4b49dd"), ObjectId("5e58e0dc436ce823fb4b49df")]
    }
])

你可能感兴趣的:(MongoDB文档之间的关系)