mongodb创建集合的时候,指定索引

余生请多指教ANT / 2023-08-26 / 原文

  IndexOptions indexOptions=new IndexOptions();
                //后台创建索引
                indexOptions.background(true);
                //唯一索引
                indexOptions.unique(true);
                //如果为true,则索引仅引用具有指定字段的文档
//                indexOptions.sparse(true);
                mongoTemplate.createCollection(collectionName).createIndex(Indexes.ascending("planId"),indexOptions);