比如说我需要在 MongoDB 中存储公司的各部门信息,结构大概是这样的:
公司
部门A
部门A属性1
部门A属性2
部门A员工
员工A1及其信息
员工A2及其信息
员工A3及其信息
员工A4及其信息
部门B
部门B属性1
部门B员工
员工B1及其信息
员工B2及其信息
员工B3及其信息
请问在 MongoDB 中应该如何存储这个结构?
> db.department.find()
{ "_id" : ObjectId( "1" ), "attrA1" : "", "attrA2" : "", "staff" : [ { "name" : "A1" } , { "name" : "A2" } ] }
{ "_id" : ObjectId( "2" ), "attrB1" : "", "attrB2" : "", "staff" : [ { "name" : "B1" } , { "name" : "B2" } ] }
还是:
> db.department.find()
{ "_id" : ObjectId( "1" ), "attrA1" : "", "attrA2" : "" }
{ "_id" : ObjectId( "2" ), "attrB1" : "", "attrB2" : "" }
>db.department.staff.find()
{ "_id" : ObjectId("3"), "name" : "A1", "department": [ { "$ref" : "department", "$id" : ObjectId( "1" ) } ] }
{ "_id" : ObjectId("4"), "name" : "A2", "department": [ { "$ref" : "department", "$id" : ObjectId( "1" ) } ] }
{ "_id" : ObjectId("5"), "name" : "B1", "department": [ { "$ref" : "department", "$id" : ObjectId( "1" ) } ] }
{ "_id" : ObjectId("6"), "name" : "B2", "department": [ { "$ref" : "department", "$id" : ObjectId( "1" ) } ] }
或者应该怎么组织这个结构才好?