笔者在mongo3.4/4.0环境下使用以下命令创建索引,
db.getCollection("xxx.com").createIndex({ mobile: "" }, { name: "mobile_mac" })
执行以下demo查询索引
mongoTemplate.indexOps(collection).getIndexInfo(); #笔者正式环境不是这么写的,这里方便大家理解,使用语法糖方式的代码 ;p
每次走到xxx.com表的时候总是报错
Exception in thread "Configuration Initializer" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.lang.Double.<init>(Double.java:608) at org.springframework.data.mongodb.core.IndexConverters.lambda$getDocumentIndexInfoConverter$1(IndexConverters.java:147) at org.springframework.data.mongodb.core.DefaultIndexOperations$1.getIndexData(DefaultIndexOperations.java:126) at org.springframework.data.mongodb.core.DefaultIndexOperations$1.doInCollection(DefaultIndexOperations.java:116) at org.springframework.data.mongodb.core.DefaultIndexOperations$1.doInCollection(DefaultIndexOperations.java:110) at org.springframework.data.mongodb.core.DefaultIndexOperations.execute(DefaultIndexOperations.java:141) at org.springframework.data.mongodb.core.DefaultIndexOperations.getIndexInfo(DefaultIndexOperations.java:110) at com.xmd.model.db.MongoDBHelper$1.run(MongoDBHelper.java:97) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
最初怀疑是框架bug,想整合新版本(2.13)进去,浪费了好久时间最终没能成功,回到了2.0.0.M1。
本地是jar版本是托管给maven,maven上没有该版本的源代码,最终硬着头皮调试。
用navicat mongo查看索引,的确是没有排序
后来想到mongo控制台创建索引有1,-1两个值,回想起来。1是正序 -1是倒序。于是到官方查询文档
db.collection.createIndex(keys, options)
For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.
到这里基本解决问题了,方法如下:
1、删除索引
db.getCollection(‘xxx.com‘).dropIndex("mobile_mac")
2、重建索引
db.getCollection("xxx.com").createIndex({ mobile: 1 }, { name: "mobile_mac" })
原文地址:https://www.cnblogs.com/passedbylove/p/10263790.html
时间: 2024-10-24 01:07:48