[JAVA-3453] MongoCollection.listIndexes() fails when there is an index with partialFilterExpression that contains DBRef Created: 07/Oct/19  Updated: 27/Oct/23  Resolved: 08/Oct/19

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 3.11.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Miika Vesti Assignee: Jeffrey Yemin
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

How to re-produce:

First, create 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}}})

Then try to list and print those indexes:

public static void main(String[] args) {
	try (MongoClient mongo = new MongoClient("localhost")) {
		MongoDatabase db = mongo.getDatabase("test");
		MongoCollection<Document> col = db.getCollection("user");
		col.listIndexes().spliterator().forEachRemaining(index -> System.out.println(index.toJson()));
	}
}

This results in exception:

Exception in thread "main" 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.writeValue(DocumentCodec.java:182)
	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)



 Comments   
Comment by Jeffrey Yemin [ 08/Oct/19 ]

Thanks cstrobl

Comment by Miika Vesti [ 08/Oct/19 ]

Thanks for the info. This ticket can be closed if this is going to get fixed in Spring Data.

Comment by Christoph Strobl [ 08/Oct/19 ]

We're going to ship a fix for Spring Data with the next service release.

Comment by Jeffrey Yemin [ 07/Oct/19 ]

The problem stems from the fact that Document#toJson uses the default DocumentCodec, which is unaware of DBRef (since it's in the bson module, but DBRef is not).

I can suggest two workarounds:

        col.listIndexes().spliterator().forEachRemaining(index ->
                System.out.println(index.toJson(MongoClientSettings.getDefaultCodecRegistry().get(Document.class))));

or

        col.listIndexes(BsonDocument.class).spliterator().forEachRemaining(index ->
                System.out.println(index.toJson()));

CC cstrobl

Comment by Miika Vesti [ 07/Oct/19 ]

I originally stumbled upon this issue when using Spring-data-mongodb library DATAMONGO-2388

Generated at Thu Feb 08 08:59:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.