Mongodb 1 to 1 embedding
I am new to developing with mongodb. This may be a noobish question but I was wondering what the benefit in creating a 1:1 embedding is in mongodb? Why is usef开发者_运维问答ul over attributes which hold 1:1 embedding's data?
In the case of 1:n embedding it is good to organize an undefined number of objects under a document.
All referenced relationships are good for queries to and from documents.
What additional functionality do 1:1 embeddings give?
Thanks
Are you thinking about the following pattern?
{
name: {
first: 'John',
second: 'James',
last: 'Jones'
},
age: 20,
address: {
street: '2nd',
city: 'Warsaw'
}
}
As you can obviously see, the reasons are pragmatic - grouping related fields (like name
and address
) together is more readable compared to a table with long flat list of columns. Also you avoid excessively long identifiers by using dot-notation.
Obviously there is a performance difference between these approaches. If nested objects are huge and you rarely use them, it will be easier to split the document (however you can fetch only part of the document). On the other hand if whole document is used most of the time, it will be cheaper to fetch it once (eagerly) instead of running multiple database queries.
Also MongoDB is not transactional per se, however modifications of a single document are atomic.
精彩评论