我有以下表格结构 - 例如Users,Projects。
用户has_many项目 项目has_many照片
但我想将项目和照片的某些基本内容嵌入到用户表中,这样我就可以减少某些页面的数据库调用次数。
以下是我提出的结构。
用户表
[
{
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha@mink7.com",
"gender": "male",
"telephone": "9986377561",
"is_pro": 1,
"projects": [
{
"id": "LONG-MONGO-ID-HERE-PROJECT-1",
"name": "Nike",
"url": "http://nike.com",
"logo": "logo_nike.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project1_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project1_photo2.jpg"
}
]
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project2_photo2.jpg"
}
]
}
]
},
{
"id": "LONG-MONGO-ID-HERE-USER-2",
"name": "Pruthvi Gowda",
"email": "pruthvi@mink7.com",
"gender": "male",
"telephone": "9982318016",
"is_pro": 0,
"projects": [
{
"id": "LONG-MONGO-ID-HERE-PROJECT-3",
"name": "Adidas",
"url": "http://adidas.com",
"logo": "logo_adidas.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project1_photo3.jpg"
},
{
"title": "another title for an Image",
"file": "project1_photo4.jpg"
}
]
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"photos": [
{
"title": "Some title for an Image",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"file": "project2_photo2.jpg"
}
]
}
]
}
]
项目表
[
{
"id": "LONG-MONGO-ID-HERE-PROJECT-1",
"name": "Nike",
"url": "http://nike.com",
"logo": "logo_nike.jpg",
"about": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"testimonial": "e middle of text. All the Lorem Ipsum generators on the Internet tend",
"photos": [
{
"title": "Some title for an Image",
"description": "um is simply dummy text of the printing and t",
"file": "project1_photo1.jpg"
},
{
"title": "another title for an Image",
"description": "text of the printing and t um is simply dummy",
"file": "project1_photo2.jpg"
}
],
"user": {
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha@mink7.com"
}
},
{
"id": "LONG-MONGO-ID-HERE-PROJECT-2",
"name": "BMW",
"url": "http://bmw.com",
"logo": "logo_bmw.jpg",
"about": "It is a long established fact that a reader will be distracted by the",
"testimonial": "from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum",
"photos": [
{
"title": "Some title for an Image",
"description": "um is simply dummy text of the printing and t",
"file": "project2_photo1.jpg"
},
{
"title": "another title for an Image",
"description": "text of the printing and t um is simply dummy",
"file": "project2_photo2.jpg"
}
],
"user": {
"id": "LONG-MONGO-ID-HERE-USER-1",
"name": "Harsha MV",
"email": "harsha@mink7.com"
}
}
]
据我所知,MongoDb完全是关于数据的复制。这是设计数据库结构的正确方法。
class User
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
field :company_name, type: String
embeds_many :projects, class_name: "Project"
has_many :projects, class_name: "Project"
end
我可以做上面这样的事情,这样我就可以保存两个相同数据的实例。 但正如您在嵌入式文档中看到的那样,我没有存储项目中的所有数据 - 如何将所有数据作为嵌入文档添加但是作为单独的表存储呢?
答案 0 :(得分:2)
无法为嵌入和引用的集合添加相同的模型。您可以通过引用用户中的项目集合将结构与最初提到的结构相同。
创建虚拟模型说ImportantProject
,它将嵌入到用户中,您可以在数据库中进行更改时同步此模型中的数据项目。