问题
Actually we want to use MongoDB for some BI processing and we don't know which schema is more suited in our case to get the job done. Imagine we got 100 000 data describing sales of a certain network, do we have to put all this data in one array? (like this)
{
"_id" : ObjectId()
"dataset" : "set1",
"values" : [
{"property":"value_1"},
.
.
.
.
{"property":"value_100000"}
]
}
Or for each entry a document? (like this)
{"_id: ObjectId(), "property":"value_1"}
.
.
.
{"_id: ObjectId(), "property":"value_100000"}
Or simply what is the ideal way to scheme this use case?
回答1:
Embedding is better for :
- Small subdocuments
- Data that does not change regularly
- WHen eventual consistency is acceptable
- Document that grow by a small amount
- Data that you'll often need to perform asecond query to fetch
- Fast reading speed
References are better for
- Large subdocuments
- Volatile data
- When immediate consitency is necessary
- Document grow with a large amount
- Data that you often exclude from document
- Fast write speed
-From 《Mongodb Definitive Guide》
Reference is something like {'_id':ObjectId("123"),'cousin':ObjectId("456")} It refers to his cousin through its ObjectId something like foreign key in SQL.
来源:https://stackoverflow.com/questions/26392285/mongodb-for-bi-use