Mongodb 1对1嵌入
我是使用 mongodb 开发的新手。这可能是一个新手问题,但我想知道在 mongodb 中创建 1:1 嵌入有什么好处?为什么比保存 1:1 嵌入数据的属性有用?
在 1:n 嵌入的情况下,最好在文档下组织未定义数量的对象。
所有引用的关系都适合对文档进行查询。
1:1 嵌入提供了哪些附加功能?
谢谢
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 useful 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正在考虑以下模式?
正如您显然所看到的,原因是务实的 - 与具有长而扁平的列列表的表格相比,将相关字段(例如
name
和address
)分组在一起更具可读性。您还可以通过使用点表示法来避免过长的标识符。显然,这些方法之间存在性能差异。如果嵌套对象很大并且您很少使用它们,则拆分文档会更容易(但是您只能获取文档的一部分)。另一方面,如果大部分时间都使用整个文档,那么(急切地)获取一次文档会比运行多个数据库查询更便宜。
此外,MongoDB 本身并不是事务性的,但单个文档的修改是原子的。
Are you thinking about the following pattern?
As you can obviously see, the reasons are pragmatic - grouping related fields (like
name
andaddress
) 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.