Jav*_*tar 7 mongodb spring-data spring-data-mongodb
假设我有以下类似的实体:
@Document(collection = "doc_a")
public class A {
@Field("id")
private Integer id;
@Field("b")
private Collection<B> b;
...
}
public class B {
@Field("id")
private Integer id;
...
}
Run Code Online (Sandbox Code Playgroud)
是否可以将A.id和B.id一起使用复合指数?
我的意思可能是:
@CompoundIndex(name = "aid_bid_idx", def = "{'id', 'b.id'}")
Run Code Online (Sandbox Code Playgroud)
提前致谢.
小智 10
我在我的应用程序中尝试过这种复合索引,它也使用了弹簧数据,并且运行正常.您只需要更正@CompoundIndex注释中的索引定义:
@CompoundIndex(name = "aid_bid_idx", def = "{'id' : 1, 'b.id' : 1}")
@Document(collection = "doc_a")
public class A {
@Field("id")
private Integer id;
@Field("b")
private Collection<B> b;
...
}
public class B {
@Field("id")
private Integer id;
...
}
Run Code Online (Sandbox Code Playgroud)
如果在mongo shell中运行带有explain的查询(如下所示),您将看到将使用索引*aid_bid_idx*.
db.doc_a.find({ "id" : 1, "b.id" : 1}).explain()
Run Code Online (Sandbox Code Playgroud)
结果将是这样的:
{
"cursor" : "BtreeCursor aid_bid_idx",
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16807 次 |
| 最近记录: |