【发布时间】:2017-07-16 00:37:26
【问题描述】:
我在 MongoDB 中为对话创建了一个模式,其中消息存储为对话对象中的对象数组。
Conversation {
company_id: { type:ObjectId, index: true },
messages: [{
_id: { type: ObjectId, index: true }
}]
}
我有一个查询,它根据数组中发送的第一条消息(从应用程序的另一部分发送)的 company_id 和 _id 查找对话。
Conversation.findOne({ company_id: c_id, messages._id: firstMessage })
理论上,如果一家公司有 1 亿个对话,并且每个对话有 100 万条消息,那么查询子文档会有多少性能问题,而不是我将第一个消息 id 存储在主文档中仅记录和查询基础对象?
Conversation {
company_id: { type:ObjectId, index: true },
firstMessage_id: { type:ObjectId, index: true },
messages: [{
_id: { type: ObjectId, index: true }
}]
}
Conversation.findOne({ company_id: c_id, firstMessage_id: firstMessage })
提前感谢您的帮助。
【问题讨论】:
标签: node.js mongodb performance mongoose mongodb-query