Announcement Announcement Module
Collapse
No announcement yet.
Indexing embedded document collection via @CompoundIndexes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Indexing embedded document collection via @CompoundIndexes

    I'm trying to index fields on an embedded collection and the indexes don't seem to be created. For example:

    Code:
    @Document
    @CompoundIndexes({
    	@CompoundIndex(name = "cred_idx", def = "{ 'credentials.provider': 1, 'credentials.providerUid': 1 }")
    })
    public class User {
    
    	@Id
    	private ObjectId id;
    	
    	@Field
    	private String name;
    	
    	@Field
    	private Set<Credential> credentials = new HashSet<Credential>();
    }
    
    public class Credential {
    
    	private String provider;
    	
    	private String providerUid;
    }
    I want to index the Credential.provider and Credential.providerUid. If I put the annotation on the Credential class, then a Credential collection is made with the proper index. However, that doesn't get me anywhere.

    Is there something I'm doing wrong, or do I need to do this by hand via the MongoTemplate?

    Thanks.

  • #2
    Figured it out. @CompoundIndex has a collection field. So the Credential object gets annotated like so:

    Code:
    @CompoundIndexes({
    	@CompoundIndex(name = "cred_idx", def = "{ 'provider': 1, 'providerUid': 1 }", collection = "user")
    })
    public class Credential {
    
     // ....
    }

    Comment

    Working...
    X