[JAVA-1261] ensureIndex is running fine but createIndex is throwing error. Created: 03/Jun/14  Updated: 13/Apr/16  Resolved: 13/Apr/16

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

Type: Bug Priority: Major - P3
Reporter: Rajesh Sharma Assignee: Ross Lawley
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Client on Windows and Server on Linux, Database is 2.4.8.


Attachments: Java Source File MongoIssueTest.java    

 Description   

When i run use DBCollection.ensureIndex() option it is creating Index without any issue but this method is depreciated and it is suggested to use createIndex method. Using createIndex is throwing the following Exception.

I am trying to create TTL Collection using following DBObjects.

BasicDBObject ttlIndexObj=new BasicDBObject("actionDate", 1);
BasicDBObject ttlIndexObjOption=new BasicDBObject("name", "actionDate_ttl_7_days").append("expireAfterSeconds", 204800);
 
com.mongodb.MongoException: not authorized to create index on 
	at com.mongodb.CommandResult.getException(CommandResult.java:100)
	at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
	at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
	at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
	at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
	at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
	at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:365)
	at com.mongodb.DBCollection.createIndex(DBCollection.java:484)
	at com.fitch.financial_statement_api.dao.NicknameChangeLogDAOTest.tesTTLIndex(NicknameChangeLogDAOTest.java:88)



 Comments   
Comment by Rajesh Sharma [ 04/Jun/14 ]

Thanks a lot for your prompt reply. Have a great day!

Comment by Ross Lawley [ 04/Jun/14 ]

Fixed in future versions of the driver

Comment by Ross Lawley [ 04/Jun/14 ]

No problems, I believe the error is caused by the missing namespace in the index options and isn't being reported back correctly. Changing the options in the testcase to:

            BasicDBObject ttlIndexObjOption = new BasicDBObject("name", "actionDate_ttl_7_days")
                                                .append("ns", "financial-1.test_collection")
                                                .append("expireAfterSeconds", 204800);

Fixes the issue. In later versions of the driver the namespace is automatically added for you.

The namespace should have the key "ns" and the value of: "<database_name>.<collection_name>"

Comment by Rajesh Sharma [ 04/Jun/14 ]

With 2.12.2 it's working fine. Behavior of createIndex is good but we are using version 2.10.1 for this project as we have other dependencies and we don't want to have any conflict. May be we will use ensureIndex and not createIndex with version 2.10.1 as createIndex is buggy in that version.

Thanks a lot!.

Comment by Ross Lawley [ 04/Jun/14 ]

rajeshks117 are you able to test against a newer version of the Java driver? 2.10.1 was released in December 2013 and the latest is now 2.12.2. There have been numerous bug fixes since then https://github.com/mongodb/mongo-java-driver/releases

Comment by Rajesh Sharma [ 04/Jun/14 ]

This is the test java file.

Comment by Rajesh Sharma [ 04/Jun/14 ]

Yes the mongo Java Driver is Version 2.10.1. Authentication was enables and i was able to authenticate myself. Here is Simple source code to replicate this. I saw a weired behaviour. createIndex will either fail with authentication issue or not create Index. ensureIndex will always succeed.

Here is test case. Also attached the file as it's showing wrong format in preview

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import java.net.UnknownHostException;
import java.util.Date;
 
import java.util.List;
import java.util.Set;
 
/*
 
 /**
 *
 * @author rsharma
 */
public class MongoIssueTest {
 
    public static void main(String[] args) {
 
        try {
 
            MongoClient mongoClient = new MongoClient("mongo-dev01", 27017);
            DB db = mongoClient.getDB("financial-1");
            boolean auth = db.authenticate("mongo-financial-usr", "********".toCharArray());
 
            System.out.println("Authenticated :" + auth);
 
            DBCollection collection = db.getCollection("test_collection"); 
            
            BasicDBObject insertDocument = new BasicDBObject();
            insertDocument.put("name", "rajesh");
            insertDocument.put("age", 30);
            insertDocument.put("actionDate", new Date());
            collection.insert(insertDocument);
        
           // for repetitive test run, drop index
            collection.dropIndexes();
            
            BasicDBObject ttlIndexObj = new BasicDBObject("actionDate", 1);
            BasicDBObject ttlIndexObjOption = new BasicDBObject("name", "actionDate_ttl_7_days").append("expireAfterSeconds", 204800);
            
            
            // change this to createIndex and you will see the behavior, either it will not create index or give you error.
            collection.ensureIndex(ttlIndexObj, ttlIndexObjOption);
            
            System.out.println(" Index :"+ collection.getIndexInfo());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
 
    }
}

Here is the output of system.users collection.

replicadev01:PRIMARY> db.system.users.find().pretty()
{
        "_id" : ObjectId("5210d2388672a50e4cb5b966"),
        "user" : "mongo-financial-usr",
        "readOnly" : false,
        "pwd" : "xxxxxxxxxxxxxxxxxxxxx"
}

Comment by Ross Lawley [ 04/Jun/14 ]

Hi rajeshks117

Thanks for the ticket, I need some more information to help me understand what is happening here as ensureIndex stores a cache of created indexes and if the index isn't in the cache then it calls createIndex itself.

Can you confirm the Java driver version you are using? 2.10.1 is marked above but ensureIndex wasn't deprecated until 2.12.0.

Can you confirm you running with authentication and have you authenticated?

Finally, could you provide a test case reproducing the error?

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