Closed
Description
Miika Vesti opened DATAMONGO-2388 and commented
IndexOperations.getIndexInfo() fails when database contains an index that has partialFilterExpression which contains DBRef, such as:
db.user.createIndex({organisation: 1, "email": 1}, {name: "org-uniq-email", unique: true, partialFilterExpression: {organisation: DBRef("organisation", ObjectId("59ce08baf264b906810fe8c5")), "email": {$exists: true}}})
Stack trace below
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef.
at org.bson.codecs.configuration.CodecCache.getOrThrow (CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get (ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get (ProvidersCodecRegistry.java:37)
at org.bson.codecs.DocumentCodec.writeValue (DocumentCodec.java:184)
at org.bson.codecs.DocumentCodec.writeMap (DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.encode (DocumentCodec.java:141)
at org.bson.codecs.DocumentCodec.encode (DocumentCodec.java:45)
at org.bson.Document.toJson (Document.java:439)
at org.bson.Document.toJson (Document.java:412)
at org.bson.Document.toJson (Document.java:399)
at org.springframework.data.mongodb.core.index.IndexInfo.indexInfoOf (IndexInfo.java:121)
at org.springframework.data.mongodb.core.DefaultIndexOperations$1.getIndexData (DefaultIndexOperations.java:196)
at org.springframework.data.mongodb.core.DefaultIndexOperations$1.doInCollection (DefaultIndexOperations.java:186)
at org.springframework.data.mongodb.core.DefaultIndexOperations$1.doInCollection (DefaultIndexOperations.java:180)
at org.springframework.data.mongodb.core.MongoTemplate.execute (MongoTemplate.java:585)
at org.springframework.data.mongodb.core.MongoTemplate.execute (MongoTemplate.java:571)
at org.springframework.data.mongodb.core.DefaultIndexOperations.execute (DefaultIndexOperations.java:211)
at org.springframework.data.mongodb.core.DefaultIndexOperations.getIndexInfo (DefaultIndexOperations.java:180)
I believe the relevant part here is IndexInfo.java
row 121:
String partialFilter = sourceDocument.containsKey("partialFilterExpression")
? ((Document) sourceDocument.get("partialFilterExpression")).toJson()
: null;
Here .toJson()
invocation fails.
One possible fix for this would be:
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry());
DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
String partialFilter = sourceDocument.containsKey("partialFilterExpression")
? ((Document) sourceDocument.get("partialFilterExpression")).toJson(codec)
: null;
Affects: 2.2.1 (Moore SR1)
Referenced from: pull request #797
Backported to: 2.2.1 (Moore SR1), 2.1.12 (Lovelace SR12)