对子文档数组中的字段进行索引
我正在尝试找出从 SQL Server 移植到 MongoDB 的消息系统的最佳设计 - 当前(在 SQL Server 中)有存储消息的树表:消息、收件箱和已发送。消息存储在消息表中,收件箱/已发送条目包含每条消息的所有收件人/发件人的条目。
现在,在 MongoDB 中,我想将这三个集合合并为一个集合,其中包含如下文档:
{
_id:
subject:
body:
sender: {memid:, name:}
recip: [{memid:, name:}, {memid:, name:}, {memid:, name:}, etc]
}
现在,我需要能够通过 memid 检索给定收件人的所有消息,并且我必须快速完成此操作,因此需要一个索引(我将拥有数亿个这样的条目)。所以,我的问题是 - 我可以按数组中文档的字段进行索引吗?
I'm trying to figure out the best design for the messaging system I'm porting from SQL Server to MongoDB - currently (in SQL Server) there are tree tables that store the message: Messages, Inbox and Sent. The message is stored in Messages table, and Inbox/Sent have entries for all recipients/senders for each message.
Now, in MongoDB I wanted to combine those three into one collection, with documents like this:
{
_id:
subject:
body:
sender: {memid:, name:}
recip: [{memid:, name:}, {memid:, name:}, {memid:, name:}, etc]
}
Now, I need to be able to retrieve all messages for a given recipient by memid and I have to do it fast, so an index is required (I will have hundreds of millions of such entries). So, my question is - can I index by a field of a document in an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此处 https://docs.mongodb.com/manual/indexes/#multikey- index
mongodb 支持按数组中文档的字段进行索引。
例子:
see here https://docs.mongodb.com/manual/indexes/#multikey-index
Index by a field of a document in an array is supported by mongodb.
Example: