Datastore

1.   The Datastore interface provides type-safe methods for accessing and storing your javobjects in MongoDB. It provides get/find/save/delete methods for working with your java objects.

2.   get methods return instance(s) of your entities by its @Id . It is really just a short-cut for using find(...) with a criteria of id values. get methods return instance(s) of your entities by its @Id :

Hotel hotel = ds.get(Hotel.class, hotelId);
 

 

3.   The find methods are a lightweight wrapper around using a Query (as with createQuery() ). As a wrapper it will return a Query , which also supports Iterable<T> and the QueryResults interface.

//use in a loop
for(Hotel hotel : ds.find(Hotel.class, "stars >", 3))
   print(hotel);

//get back as a list
List<Hotel> hotels = ds.find(Hotel.class, "stars >", 3).asList();

//sort the results
List<Hotel> hotels = ds.find(Hotel.class, "stars >", 3).sort("-stars").asList();

//get the first matching hotel, by querying with a limit(1)
Hotel gsHotel = ds.find(Hotel.class, "name", "Grand Sierra").get();

//same as
Hotel gsHotel = ds.find(Hotel.class, "name =", "Grand Sierra").get();
 

4.  The list of valid operators: ["=", "==","!=", "<>", ">", "<", ">=", "<=", "in", "nin", "all", "size", "exists"] . If no operator is used, "=" is assumed. "=" is the same as "==", "!=" is the same as "<>".

5.   @Id field is filled in for you (after the save ), if you didn't set it before you call save method.

6.   The delete method will delete items based on a Query , id, or an entity:

ds.delete(Hotel.class, "Grand Sierra Resort");
//use a query
ds.delete(ds.createQuery(Hotel.class).filter("pendingDelete", true));
 

 

7.   One of the underlying features in MongoDB is the ability to do some operations in an atomic fashion. The FindAndDelete method will find the first item, and delete it (while returning it).

Hotel grandSierra = ds.findAndDelete(ds.get(Hotel.class, "Grand Sierra Resort"));

8.   Updates are applied at the server and allow for complicated but very efficient (when compared to saving the whole entity again) update operations.

long now = System.currentTimeMillis();
UpdateOperations<User> ops = datastore.createUpdateOperations(User.class).set("lastLogin", now);

Query<User> query = datastore.createQuery(User.class).field(Mapper.ID_KEY).equal(id);

ds.update(query , ops);
     

9.   ensureIndexes and ensureCaps methods should be called after you have registered you entities with Morphia . It will then synchronously , by default, create your indexes, and capped collections. One option is call them each time you start your application, or via an administrative interface, or during a deployment script. It might be best to make sure indexes can be built in the background if there is already data . Doing this on an existing system, with existing indexes and capped collections, should take no time (and do nothing). If the collection isn't capped, or has different setting you will get an error in the logs, but nothing will be done to the existing collections.

ds.ensureIndexes(); //creates all defined with @Indexed
ds.ensureCaps(); //creates all collections for @Entity(cap=@CappedAt(...))
 

 

10.   If you have an index on a capped collection, then ensuring the index will create the collection uncapped if it did not already exist. So you need to call ensureCaps first.

猜你喜欢

转载自seanzhou.iteye.com/blog/1663214
今日推荐