Results 1 to 3 of 3

Thread: MongoDB: Indexed fields are not indexed when using another collection name

  1. #1
    Join Date
    Aug 2007
    Posts
    5

    Default MongoDB: Indexed fields are not indexed when using another collection name

    When I annotate my fields with org.springframework.data.mongodb.core.index.Indexe d it works, as long as I use the default collection name, but if I do all my operations (save, find, remove etc) on another collection name it gets created but without the indexes.

    Also, a default collection (class name) always gets created, even though all operations uses an alternative collection.

    Where do I file a bug for those issues? Can anyone do it in JIRA or are there some other way? I am using mongodb 1.0.0.M4.

    Regards,
    Andreas

  2. #2
    Join Date
    Dec 2008
    Location
    Aurora, CO, USA
    Posts
    24

    Default

    I think you just need to annotate the class with the collection you want it to use.

    Code:
    @Document(collection = "impressions")
    public class Impression {
    
    @Id
    private ObjectId id;
    
    @Indexed
    private String name;
    What I think happens is it scans the annotations at startup, and creates the collections and indexes at that time.

  3. #3
    Join Date
    Aug 2004
    Posts
    18

    Default

    The solution with @Document(collection = "impressions") works, if you store the object only in one collection and the name of the collection is different than the default name. If you store an object in multiple collections you have to create the indexes manually. Maybe in such a scenario the annotation approach does not provide any benefit.

    Code:
    public class UserString {
    	private String id;
    	private String userName;
    }
    
    mongoTemplate.ensureIndex(new Index("userName", Order.ASCENDING), "customer1.users");
    mongoTemplate.ensureIndex(new Index("userName", Order.ASCENDING), "customer2.users");
    
    mongoTemplate.save(new User("ralph"), "customer1.users");
    mongoTemplate.save(new User("stefan"), "customer1.users");
    
    mongoTemplate.save(new User("john"), "customer2.users");
    mongoTemplate.save(new User("adam"), "customer2.users");

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •