【发布时间】:2015-10-11 19:47:16
【问题描述】:
给定一组如下所示的文档,如果子文档中有不确定数量的路径,在顶级排序键上创建排序索引的最佳策略是什么?
{_id: 1, title: "Document 1", sort:{somePathA: 3, somePathB: 1, somePathC: 3}}
{_id: 2, title: "Document 2", sort:{somePathA: 1, somePathB: 2, somePathC: 2}}
{_id: 3, title: "Document 3", sort:{somePathA: 2, somePathB: 3, somePathC: 1}}
例如以下命令:
db.find().sort({'sort.somePathC': 1});
应该产生以下输出:
{_id: 3, title: "Document 3", sort:{somePathA: 2, somePathB: 3, somePathC: 1}}
{_id: 2, title: "Document 2", sort:{somePathA: 1, somePathB: 2, somePathC: 2}}
{_id: 1, title: "Document 1", sort:{somePathA: 3, somePathB: 1, somePathC: 3}}
【问题讨论】:
-
你的意思是,
sort嵌入的文档字段可以是一个带有变量 nr 字段的文档? -
是的,就是这样。此外,嵌入的排序文档可能因文档而异。
标签: mongodb optimization indexing