My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for

FrequentlyAskedQuestions  
Frequently Asked Questions
Featured
Updated Dec 6, 2010 by scotthernandez

Frequently Asked Questions

Why do I need to change my @Id field from String to ObjectId

If you wanted an automatically generated id (the default _id value for mongodb) in versions prior to 0.95 you could do this:

@Id String id;

Or make queries using a string version of your ObjectId value because we automatically tried to convert any string into an ObjectId for you.

After version 0.95 this is no longer the case. You now should change that definition above from String to ObjectId like this:

@Id ObjectId id;

There are many reasons to do this. Always trying to convert from string->objectid was confusing (in documentation), and ambiguous in certain cases and made it impossible to use a string value between 18-24 characters long with alpha-numeric values (a hex string). It also was confusing because you had to remember that in other languages and the shell you had to use an ObjectId.

To simplify things and make it more transparent we have remove this implicit conversion.

If you don't want to use an ObjectId type you can manage your @Id value yourself. There is a sample class that uses an auto-incrementing long (stored as a counter on the server) as an example. Natural keys are great things to use as your id values (immutable values like a username, or email are an example in some applications).

Do you have to close connections?

Not really. The Mongo driver keeps a connection pool per Mongo instance. If you wish to release those resources, just make sure you stop using the Mongo instance.

How can I use data that is already in MongoDB (like from another framework/mapper)?

If you are starting with data and you want to make sure you build your java POJO in a way you can load the data below are a few tips.

  1. @AlsoLoad("old_name") (if you don't want to save the old format)
  2. {
    _id:1,
    desc:null,
    created_at:ISODate("2010-12-06T23:52:50.258Z"),
    updated_at:ISODate("2010-12-06T24:51:34.132Z")
    }
    class MyEntity {
      @Id long id;
    
      String desc;
    
      @AlsoLoad("thumbnail_filename")
      String thumbnail;
    
      @AlsoLoad("created_at")
      Date dateCreated = new Date();
    
      @AlsoLoad("updated_at")
      Date dateUpdated;
    }
  3. @Property("old_name")/@Embedded("old_name")
  4. class MyEntity {
      @Id long id;
    
      String desc;
    
      @Property("thumbnail_filename")
      String thumbnail;
    
      @Property("created_at")
      Date dateCreated = new Date();
    
      @Property("updated_at")
      Date dateUpdated;
    }

Should I use a capped collection for referenced/linked entities?

You can but a reference is made up of the collection name + the _id field value. In capped collections the _id field is not unique and references might be ambiguous.

Huh, why is there a className field in my Document? Can I remove it?

Yes, you can; look here

Please be careful that you follow the ClassPerCollection pattern if you remove the className from your persisted entites.

Question about life cycle methods

Absolutely:

Comment by Gallego....@gmail.com, Apr 25, 2012

are you guys planning to support the aggregation framework.


Sign in to add a comment
Powered by Google Project Hosting