Announcement Announcement Module
Collapse
No announcement yet.
MongoDB: Indexed fields are not indexed when using another collection name Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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");

      Comment

      Working...
      X