mongodb - MongoDb:如何为 Java 中的复合索引设置 IndexOptions 背景为真
问题描述
对于 mongodb 中的单字段索引,我们可以设置 IndexOptions 如下
collection.createIndex(
Indexes.ascending(actualIndexFieldName), new IndexOptions().background(true));
但不确定如何在 java 中为复合索引设置 IndexOptions。
提前致谢
解决方案
是的你可以。
java.lang.String createIndex(Bson keys,
IndexOptions indexOptions)
Create an index with the given keys and options.
Parameters:
keys - an object describing the index key(s), which may not be null.
indexOptions - the options for the index
使用Indexes
public static Bson compoundIndex(java.util.List<? extends Bson> indexes)
ArrayList<Document> indexes = new ArrayList<Document>();
indexes.add(Indexes.descending("stars"));
indexes.add(Indexes.ascending("name"));
collection.createIndex(indexes, indexOptions);
推荐阅读
- javascript - Symfony v3.4 和 jsTree v3.3.5 和 JSON 格式的数据
- java - 如何在 Springboot 中针对多个基本 ldap dns 进行验证
- ajax - Laravel Passport 为 Guzzle HTTP 客户端创建新的 API 令牌
- json - 在 Swift 中使用不记名令牌和 json 正文发送发布请求
- ios - 搜索结果迅速转移到其他视图控制器
- sql-server - 如何检索截断记录 (SQL Server) 。可行吗?
- java - 尝试创建此矩阵时不断出现错误?
- php - 如何一一获取 ACF 转发器字段值?
- javascript - 当试图在数组中存储一个值时,它没有被正确添加
- php - 逐步获取查询结果codeigniter