[JAVA-2124] Regression in DBCollection for retrieving Index information Created: 26/Feb/16  Updated: 11/Sep/19  Resolved: 15/Sep/16

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

Type: Task Priority: Major - P3
Reporter: Brandon Vulaj [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Most of the information is here: http://stackoverflow.com/questions/35659940/mongo-java-client-dbcollection-doesnt-return-whether-an-index-is-sparse-or-no

But I'll also paste below..

After recently upgrading the Java Mongo Driver from 2.6 to 3.2, I've noticed that I can no longer see if an index is sparse or not in DBCollection. On top of that, I don't think it's even creating sparse indexes.

For example:

        DBCollection entityCollection = db.getCollection("testCollectionIndex2");
 
        DBObject newIndex = new BasicDBObject();
        newIndex.put("field1", -1);
        BasicDBObject options = new BasicDBObject("unique", true);
        options.append("sparse", true);
        options.append("name", "testIndex");
        entityCollection.createIndex(newIndex, options);

Previously to upgrading, the following test has passed:

        entityCollection = db.getCollection("testCollectionIndex2");
        // index 0 is _id
        DBObject mongoIndex = entityCollection.getIndexInfo().get(1);
        Assert.assertEquals(Boolean.TRUE, mongoIndex.get("sparse"));

However, now the `sparse` field doesn't exist on the Index nor the returned JSON, at least as far as I can see in the Java driver.

How can I verify if an index is `sparse` now, using DBCollection?

This test is running against a 2.6 embedded MongoDB.

I'll note that I updated my test, and my test only, to use the newer classes (MongoCollection) to create and retrieve indexes, and it seems to work. So, I have to assume that this is a regression in the DBCollection classes.



 Comments   
Comment by Jeffrey Yemin [ 01/Mar/16 ]

Looking at the 3.x driver code, I can confirm what you're seeing. While it is a behavioral change in the driver, in practice the index will continue to function correctly, since false is the default value for sparse. But you'll have to modify the assertion to make it pass. Something like: assertTrue(!mongoIndex.containsField("sparse") || !mongoIndex.get("sparse")).

Comment by Brandon Vulaj [X] [ 29/Feb/16 ]

Hi Jeff,
Thanks for looking at that for me. You're correct on the test above. That seems to work when explicitly set to true. Unfortunately it looks like I referenced the wrong snippet here.

What we saw before was `"sparse" : false` existing when it was explicitly set to false. Rather than not seeing a `sparse` field at all. See below:

-            options.append("sparse", true);
+           options.append("sparse", false);
...
-            Assert.assertEquals(Boolean.TRUE, mongoIndex.get("sparse"));
+           Assert.assertEquals(Boolean.FALSE, mongoIndex.get("sparse"));

Can you confirm that? Mongo server version is 2.6.8.

Comment by Jeffrey Yemin [ 27/Feb/16 ]

I ran a slightly modified version of your code against a 3.2 server, using the 3.2.2 driver:

        MongoClient client = new MongoClient();
        DB db = client.getDB("test");
        DBCollection entityCollection = db.getCollection("testCollectionIndex2");
 
        // DROP THE COLLECTION TO ENSURE THAT ANY EXISTING INDEXES ARE DROPPED
        entityCollection.drop();
 
        DBObject newIndex = new BasicDBObject();
        newIndex.put("field1", -1);
        BasicDBObject options = new BasicDBObject("unique", true);
        options.append("sparse", true);
        options.append("name", "testIndex");
        entityCollection.createIndex(newIndex, options);
 
        entityCollection = db.getCollection("testCollectionIndex2");
        for (DBObject mongoIndex : entityCollection.getIndexInfo()) {
            if (mongoIndex.get("name").equals("testIndex")) {
                System.out.println(mongoIndex);
                if (mongoIndex.containsField("sparse") && (Boolean) mongoIndex.get("sparse") == true) {
                    System.out.println("Index is sparse");
                } else {
                    System.out.println("Index is not sparse");
                }
            }
        }

and the output is:

{ "v" : 1 , "unique" : true , "key" : { "field1" : -1} , "name" : "testIndex" , "ns" : "test.testCollectionIndex2" , "sparse" : true}
Index is sparse

Couple of questions:

  1. What version of the server are you using?
  2. Did you ensure that the index didn't already exist before running the test?
Generated at Thu Feb 08 08:56:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.