本文整理汇总了Java中com.google.code.morphia.query.Query类的典型用法代码示例。如果您正苦于以下问题:Java Query类的具体用法?Java Query怎么用?Java Query使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Query类属于com.google.code.morphia.query包,在下文中一共展示了Query类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: update
import com.google.code.morphia.query.Query; //导入依赖的package包/类
private <T> UpdateResults<T> update(Query<T> query, DBObject u, boolean createIfMissing, boolean multi, WriteConcern wc) {
QueryImpl<T> qi = (QueryImpl<T>) query;
DBCollection dbColl = qi.getCollection();
if (qi.getSortObject() != null && qi.getSortObject().keySet() != null && !qi.getSortObject().keySet().isEmpty())
throw new QueryException("sorting is not allowed for updates.");
if (qi.getOffset() > 0)
throw new QueryException("a query offset is not allowed for updates.");
if (qi.getLimit() > 0)
throw new QueryException("a query limit is not allowed for updates.");
DBObject q = qi.getQueryObject();
if (q == null)
q = new BasicDBObject();
WriteResult wr;
if (wc == null)
wr = dbColl.update(q, u, createIfMissing, multi);
else
wr = dbColl.update(q, u, createIfMissing, multi, wc);
throwOnError(wc, wr);
return new UpdateResults<T>(wr);
}
示例2: findByCommandBins
import com.google.code.morphia.query.Query; //导入依赖的package包/类
public List<CommandEntry> findByCommandBins(List<String> commandBins) throws Exception {
if (commandBins != null && !commandBins.isEmpty()) {
int retry = 0;
while (retry < mongodb_retrys) {
try {
Query<CommandEntry> query = ds.createQuery(CommandEntry.class);
List<CommandEntry> list = query.field("commandBin").hasAnyOf(commandBins).order( "-dateFinished, -dateLaunched" ).asList();
return list;
} catch (Exception ex) {
retry++;
logger.debug("MongoDB error. Retry: {}. Ex: {}", retry, ex.getMessage());
if (retry >= mongodb_retrys)
throw ex;
try {
Thread.sleep(200);
} catch (Exception _ex){}
}
}
logger.error("Error. findByCommandBin failed after {} retrys", retry);
return null;
} else
return null;
}
示例3: tryVersionedUpdate
import com.google.code.morphia.query.Query; //导入依赖的package包/类
@Override
protected <T> WriteResult tryVersionedUpdate(DBCollection dbColl, T entity, DBObject dbObj, WriteConcern wc, DB db, MappedClass mc) {
WriteResult wr = null;
if (mc.getFieldsAnnotatedWith(Version.class).isEmpty())
return wr;
MappedField mfVersion = mc.getFieldsAnnotatedWith(Version.class).get(0);
String versionKeyName = mfVersion.getNameToStore();
Long oldVersion = (Long) mfVersion.getFieldValue(entity);
long newVersion = VersionHelper.nextValue(oldVersion);
dbObj.put(versionKeyName, newVersion);
if (oldVersion != null && oldVersion > 0) {
Object idValue = dbObj.get(Mapper.ID_KEY);
Object shardKey = dbObj.get(SHARD_KEY_FIELDNAME);
@SuppressWarnings("unchecked")
final Query<T> filter = find((Class<T>) entity.getClass(), Mapper.ID_KEY, idValue).filter(versionKeyName, oldVersion);
if (shardKey != null) {
filter.filter(SHARD_KEY_FIELDNAME, shardKey);
}
UpdateResults<T> res = update(filter, dbObj, false, false, wc);
wr = res.getWriteResult();
if (res.getUpdatedCount() != 1)
throw new ConcurrentModificationException("Entity of class " + entity.getClass().getName() + " (id='" + idValue + "',version='" + oldVersion + "') was concurrently updated.");
} else if (wc == null)
wr = dbColl.save(dbObj);
else
wr = dbColl.save(dbObj, wc);
// update the version.
mfVersion.setFieldValue(entity, newVersion);
return wr;
}
示例4: createAutoIncrement
import com.google.code.morphia.query.Query; //导入依赖的package包/类
protected Long createAutoIncrement() {
String collName = getCollection().getName();
Query<LongIdEntity.StoredId> q = ds.find(LongIdEntity.StoredId.class, "_id", collName);
UpdateOperations<LongIdEntity.StoredId> uOps = ds.createUpdateOperations(LongIdEntity.StoredId.class).inc("value");
LongIdEntity.StoredId newId = ds.findAndModify(q, uOps);
if (newId == null) {
newId = new LongIdEntity.StoredId(collName);
ds.save(newId);
}
return newId.getValue();
}
示例5: createIDQuery
import com.google.code.morphia.query.Query; //导入依赖的package包/类
protected Query<T> createIDQuery(T ent, Query<T> q) {
MappedClass mc = mapper.getMappedClass(ent);
q.disableValidation().filter(Mapper.ID_KEY, mapper.getId(ent));
if (mc.getFieldsAnnotatedWith(ShardKey.class).size() > 0) {
MappedField shardKeyMF = mc.getFieldsAnnotatedWith(ShardKey.class).get(0);
Integer shardKey = (Integer) shardKeyMF.getFieldValue(ent);
q.filter(shardKeyMF.getNameToStore(), shardKey);
}
return q;
}
示例6: findByName
import com.google.code.morphia.query.Query; //导入依赖的package包/类
public QueryResults<Person> findByName(String name) {
Query q = getDatastore().createQuery(Person.class).field("name").containsIgnoreCase(name);
return find(q);
}
示例7: findAndModify
import com.google.code.morphia.query.Query; //导入依赖的package包/类
public T findAndModify(Query<T> q, UpdateOperations<T> ops) {
final T ret = ds.findAndModify(q, ops, false);
return ret;
}
示例8: update
import com.google.code.morphia.query.Query; //导入依赖的package包/类
public UpdateResults<T> update(T ent, Query<T> q, UpdateOperations<T> ops) {
q = createIDQuery(ent, q);
UpdateResults<T> updateResults = ds.update(q, ops);
return updateResults;
}
示例9: get
import com.google.code.morphia.query.Query; //导入依赖的package包/类
@Override
public User get(ObjectId userId) {
final Query<User> query = createUserIDQuery(userId);
return super.findOne(query);
}
示例10: delete
import com.google.code.morphia.query.Query; //导入依赖的package包/类
@Override
public WriteResult delete(User entity) {
final Query<User> query = createIDQuery(entity);
return super.deleteByQuery(query);
}
示例11: createUserIDQuery
import com.google.code.morphia.query.Query; //导入依赖的package包/类
private Query<User> createUserIDQuery(ObjectId userId) {
return createQuery().filter("_id", userId).filter("shardKey", User.getObjectIdShardKey(userId));
}
示例12: findAll
import com.google.code.morphia.query.Query; //导入依赖的package包/类
public List<User> findAll() {
final Query<User> query = createQuery().queryNonPrimary();
return find(query).asList();
}