How to use CompoundIndex in spring mongo

1,525 views
Skip to first unread message

Senthil Kumaran

unread,
Sep 30, 2014, 5:55:42 AM9/30/14
to mongod...@googlegroups.com


Hi,

I have creating index in mongo database then i was used  compound index like this code

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")

@CompoundIndexes({
   @CompoundIndex(name = "Testing", def = "{'status' : 1,'listType':1,'access':1}")
})
   
public class User {

    @Id
    private String id;

    String username;

    String password;
    private String status;
    private String listType;
    private String access;

 getter and setter method

}

but does not find any different by using index while fetching 100000 records.please anyone suggestion for this problem


Thanks

senthilkumaran

Will Berkeley

unread,
Oct 7, 2014, 4:11:01 PM10/7/14
to mongod...@googlegroups.com
I don't understand what you mean by "does not find any different by using index". Do you mean that the compound index isn't used for your query? What is the query that you expect to use the index? Have you run .explain() on it? Please post the query and the explain output along with the output of db.users.getIndexes() from the mongo shell.

-Will
Message has been deleted

Senthil Kumaran

unread,
Oct 15, 2014, 1:57:58 AM10/15/14
to mongod...@googlegroups.com
Hi,

I have creating index in mongo database then i was used  compound index following this steps :

Step 1:

import org.springframework.data.annotation.Id;
import org.springframework.data.
mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")

@CompoundIndexes({
   @CompoundIndex(name = "Testing", def = "{'status' : 1,'listType':1,'access':1}")
})
   
public class User {

    @Id
    private String id;

    String username;

    String password;
    private String status;
    private String listType;
    private String access;

 public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

   

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + ", status=" + status + ", listType=" + listType
                + ", access=" + access + "]";
    }

    public User( String username, String password, String status,
            String listType, String access) {
        super();
        this.username = username;
        this.password = password;
        this.status = status;
        this.listType = listType;
        this.access = access;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getAccess() {
        return access;
    }

    public void setAccess(String access) {
        this.access = access;
    }

    public String getListType() {
        return listType;
    }

    public void setListType(String listType) {
        this.listType = listType;
    }


}


Step 2:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo host="localhost" port="27017" />
    <mongo:db-factory dbname="Testdb" />

    <bean id="mongoTemplate" class="org.springframework.
data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

</beans>

Step 3:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

    @Override
    public String getDatabaseName() {
        return "Testdb";
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost");
    }
}

Step 4:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig1 {

    public @Bean
    MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(), "Testdb");
    }

    public @Bean
    MongoTemplate mongoTemplate() throws Exception {
       
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
               
        return mongoTemplate;
       
    }

}

Step 5:

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mkyong.config.SpringMongoConfig;
import com.mkyong.model.User;
//import org.springframework.context.support.GenericXmlApplicationContext;

public class App {
   
    public static void main(String[] args)
    {
   
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
    // For XML
   //ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
  // For Annotation
       
        for(int i=0;i<100000 ;i++)
        {

        User user = new User("mkyong"+i, "password123","I","music","public");

        // save
        mongoOperation.save(user);

        // now user object got the created id.
        System.out.println(i+". user : " + user);
        }
       
        Query query=new Query(Criteria.where("status").is("A").and("listType").is("music").and("access").is("public"));
        List<User> listUser = mongoOperation.find(query,User.class);
        System.out.println("4. Number of user = " + listUser.size());

       
    }

   
       
   

}

but does not find any different by using index while fetching 100000 records.please anyone suggestion for this problem


Thanks
senthilkumaran
Reply all
Reply to author
Forward
0 new messages