我有一个MongoDB文档,结构类似于下面的结构。这些文档中有大量的文档,其中包含数千人,因此我希望通过在人员上添加索引来加快速度。#search_columns.surname和people。#。search_columns.givenname。 Howe我可以在MongoDB中这样做吗?谢谢你的帮助。
[_id] => MongoId Object (
[$id] => 53b1b1ab72f4f852140dbdc9
)
[name] => People From 1921
[people] => Array (
[0] => Array (
[name] => Barada, Valentine
[search_columns] => Array (
[surname] => Array (
[0] => Mardan,
[1] => Barada
)
[givenname] => Array (
[0] => Valentine
)
)
)
[1] => Array (
[name] => Barsaloux, Nicholas
[search_columns] => Array (
[surname] => Array (
[1] => Barsaloux
)
[givenname] => Array (
[0] => Nicholas
)
[place] => Array (
)
)
)
)
答案 0 :(得分:3)
您可以使用点表示法为嵌套字段创建索引:
db.collection.ensureIndex({'people.search_columns.surname': 1});
db.collection.ensureIndex({'people.search_columns.givenname': 1});