MongoDB 中的嵌入文档
Embedded Documents in MongoDB
我无法准确理解嵌入式文档是如何在 MongoDB 等软模式中实施的。如果给出这样的数据模型:
User{
UserId:
Email:
Phone:
Reviews{
Rating:
Comment:
}//End reviews
}
这是否意味着每个用户文档只有一个评论,或者这是所有评论都将用于一个用户的格式?
也许更相关的模型如下:
User{
UserId:
Phone:
Email:
}
Reviews{
User:<UserID>
Rating:
Comment:
}
我知道使用引用意味着查询速度较慢,但我不明白嵌入式文档如何允许多次审查。
您是在谈论如何在 MongoDB 或像 Mongoose 这样的 ODM 中执行此操作?对于 MongoDB,答案很简单:MongoDB 不强制执行架构。您可以在 MongoDB 集合中包含以下所有三个文档:
{
"reviews" : {
"rating" : 9001,
"comment" : "nice scenery"
}
}
{
"reviews" : [
{
"rating" : [2, 4, 5],
"comment" : 19
},
{
"rating" : -845,
"comment" : "Powerful stuff"
}
]
}
{
"reviews" : "no"
}
在强制执行架构应用程序端的 ODM 中,您的表示法最接近意味着每个文档应该有一个 reviews
子文档的表示法。
在同一用户有多个文档的情况下,MongoDB 会在您每次添加新文档时向文档添加一个唯一的 _Id,因此当您为同一个用户添加多个文档时这很有效用户,正如@wdberkeley 所说,MongoDB 不强制执行模式,
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my first comment"
}
}
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my second comment"
}
}
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my third comment"
}
}
如果一个用户的一份文档有多个评论,这也可以正常工作,总是可以获取文档并向其添加附加评论,然后在文档上调用 db.update(),
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Reviews"{[
{
"Rating": "5"
"Comment": "this is my first comment"
},
{
"Rating": "5"
"Comment": "this is my second comment"
},
{
"Rating": "5"
"Comment": "this is my third comment"
}
]}
}
我无法准确理解嵌入式文档是如何在 MongoDB 等软模式中实施的。如果给出这样的数据模型:
User{
UserId:
Email:
Phone:
Reviews{
Rating:
Comment:
}//End reviews
}
这是否意味着每个用户文档只有一个评论,或者这是所有评论都将用于一个用户的格式?
也许更相关的模型如下:
User{
UserId:
Phone:
Email:
}
Reviews{
User:<UserID>
Rating:
Comment:
}
我知道使用引用意味着查询速度较慢,但我不明白嵌入式文档如何允许多次审查。
您是在谈论如何在 MongoDB 或像 Mongoose 这样的 ODM 中执行此操作?对于 MongoDB,答案很简单:MongoDB 不强制执行架构。您可以在 MongoDB 集合中包含以下所有三个文档:
{
"reviews" : {
"rating" : 9001,
"comment" : "nice scenery"
}
}
{
"reviews" : [
{
"rating" : [2, 4, 5],
"comment" : 19
},
{
"rating" : -845,
"comment" : "Powerful stuff"
}
]
}
{
"reviews" : "no"
}
在强制执行架构应用程序端的 ODM 中,您的表示法最接近意味着每个文档应该有一个 reviews
子文档的表示法。
在同一用户有多个文档的情况下,MongoDB 会在您每次添加新文档时向文档添加一个唯一的 _Id,因此当您为同一个用户添加多个文档时这很有效用户,正如@wdberkeley 所说,MongoDB 不强制执行模式,
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my first comment"
}
}
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my second comment"
}
}
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Review"{
"Rating": "5"
"Comment": "this is my third comment"
}
}
如果一个用户的一份文档有多个评论,这也可以正常工作,总是可以获取文档并向其添加附加评论,然后在文档上调用 db.update(),
"User" {
"UserId": "johndoe"
"Email": "johndoe@yahoo.com"
"Phone": "555-44444"
"Reviews"{[
{
"Rating": "5"
"Comment": "this is my first comment"
},
{
"Rating": "5"
"Comment": "this is my second comment"
},
{
"Rating": "5"
"Comment": "this is my third comment"
}
]}
}