读书人

Mongo的ORM框架的学习Morphia(十二) m

发布时间: 2012-08-28 12:37:01 作者: rapoo

Mongo的ORM框架的学习Morphia(十二) morphia的Query和Update

morphia的Update

?

??

?

?

??

初始化:

Problems in the data type (comparing the field type and parameter type) are logged as warnings since it is possible that the server can coerce the values, or that you meant to send something which didn't seem to make sense; The server uses the byte representation of the parameter so some values can match even if the data types are different (numbers for example).

Disabling Validation

Validation can be disabled by calling disableValidation() as the beginning of the query definition, or anywhere within you query.

Datastore ds = ...? Query q = ds.createQuery(MyEntity.class).disableValidation();? ? //or it can be disabled for just one filter? ? Query q = ds.createQuery(MyEntity.class).disableValidation().filter("someOldField", value).enableValidation().filter("realField", otherVal);

?

?

Sort 

You can sort by a field, or multiple fields in ascending or descending order.

Datastore ds = ...? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).order("dateAdded");? ... // desc order? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).order("-dateAdded");? ... // asc dateAdded, desc foo? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).order("dateAdded, -foo");

?

?

Limit 

You can also limit for the number of elements.

Datastore ds = ...? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).limit(100);

?

?

Offset (skip) 

You can also ask the server to skip over a number of elements on the server by specifying an offset value for the query. This will less efficient than a range filter using some field, for pagination for example.

Datastore ds = ...? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).offset(1000);

?

?

Ignoring Fields 

MongoDB also supports only returning certain fields. This is a little strange in application but it is an important way to trim parts off of embedded graphs. This will lead to partial entities and should be used sparingly, if at all.

Datastore ds = ...? MyEntity e = ds.createQuery(MyEntity.class).retrievedFields(true, "foo").get();? ? val = e.getFoo(); // only field returned? ? ...? ? MyEntity e = ds.createQuery(MyEntity.class).retrievedFields(false, "foo").get();? ? val = e.getFoo(); // only field not returned

?

?

The field name argument (the last arg) can be a list of strings or a string array:

MyEntity e = ds.createQuery(MyEntity.class).retrievedFields(true, "foo", "bar").get();? ? val = e.getFoo(); // fields returned? vak = e.getBar(); // fields returned

?

?

Returning Data 

To return your data just call one of the QueryResults methods. None of these methods affect the Query. They will leave the Query alone so you can continue to use it to retrieve new results by calling these methods again.

?

methoddoesget() returns the first Entity -- using limit(1) asList() return all items in a List -- could be costly with large result sets fetch() explicit method to get Iterable instance asKeyList() return all items in a List of their Key<T> -- This only retrieves the id field from the server. fetchEmptyEntities() Just like a fetch() but only retrieves, and fills in the id field.

Datastore ds = ...? Query q = ds.createQuery(MyEntity.class).filter("foo >", 12);? ? //single entity? MyEntity e = q.get();? ? e = q.sort("foo").get();? ? //for? for (MyEntity e : q)? ? print(e);? ? //list? List<MyEntity> entities = q.asList();

?

?

 

读书人网 >PowerDesigner

热点推荐