Announcement Announcement Module
No announcement yet.
Mongo newbie questions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mongo newbie questions

    1. I have an id field, but I want its value to be autogenerated. How can i specify that?

    2. How to create an index on an embedded document?
    Example: class A {List<B> bList;}
    class B{int b;}
    The JSON looks like
    ({bList : [ {b : b1}, {b: b2}]

    I want to create index on the JSON field bList.b.
    (Using @Index on the B.b attribute doesn't work; I can't understand what index that creates, but it's not the one I want.)

    3. What's the best way to use multiple databases? (The database-wide lock in mongodb may require me to put write-heavy collections in a separate database).

    If any of the above questions doesn't make sense, please let me know. I'm new to both Mongo and Spring.


  • #2
    Ans to Q2: Looks like I can create this using @CompoundIndex with a def of {'bList.b' : 1}.
    (From the documentation, I'd thought that I should use that only for compound indexes).


    • #3
      Elaboration to Q1: From the docs, "As such, for autogeneration of an ObjectId to succeed the type of the Id property/field in your
      class must be either a String, ObjectId, or BigInteger." But if I'm moving from relational to mongodb, I might typically have classes that have an "id" attribute of type, say, "long". In such a case, while moving to mongodb, would I have to add a new attribute ("newId") and mark that with @Id? And the existing "id" would have to be scrapped, eh?
      The big issue is that there's an API change; guys talking to a long id, will now have to change to talk to, say, a BigInteger newId unless I do an internal conversion to preserve the API.

      Is there an easier/cleaner way?