本文整理汇总了Java中org.mongodb.morphia.query.Query类的典型用法代码示例。如果您正苦于以下问题:Java Query类的具体用法?Java Query怎么用?Java Query使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Query类属于org.mongodb.morphia.query包,在下文中一共展示了Query类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: search
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
/**
* A search function
*
* @param date
* @param width
* @param height
* @param count
* @param offset
* @return
*/
public List<M> search(String datefield, Date date, int width, int height, int count, int offset, UserAccount account, String query, List<String> sources) {
List<Criteria> l = new ArrayList<>();
Query<M> q = getDatastore().createQuery(clazz);
if (query != null) {
Pattern p = Pattern.compile("(.*)" + query + "(.*)", Pattern.CASE_INSENSITIVE);
l.add(q.or(
q.criteria("title").equal(p),
q.criteria("description").equal(p)
));
}
if (account != null) {
l.add(q.criteria("contributor").equal(account));
}
if (width > 0)
l.add(q.criteria("width").greaterThanOrEq(width));
if (height > 0)
l.add(q.criteria("height").greaterThanOrEq(height));
if (date != null)
l.add(q.criteria(datefield).greaterThanOrEq(date));
if (sources != null)
l.add(q.criteria("source").in(sources));
q.and(l.toArray(new Criteria[l.size()]));
return q.order("crawlDate").offset(offset).limit(count).asList();
}
示例2: findAndDelete
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Override
public <T> T findAndDelete(final Query<T> query, final FindAndModifyOptions options) {
DBCollection dbColl = query.getCollection();
if (dbColl == null) {
dbColl = getCollection(query.getEntityClass());
}
if (LOG.isTraceEnabled()) {
LOG.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ...");
}
FindAndModifyOptions copy = enforceWriteConcern(options, query.getEntityClass())
.copy()
.projection(query.getFieldsObject())
.sort(query.getSortObject())
.returnNew(false)
.upsert(false)
.remove(true);
final DBObject result = dbColl.findAndModify(query.getQueryObject(), copy.getOptions());
return result == null ? null : mapper.fromDBObject(this, query.getEntityClass(), result, createCache());
}
示例3: testDottedNames
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Test
public void testDottedNames() {
ParentType parentType = new ParentType();
EmbeddedSubtype embedded = new EmbeddedSubtype();
embedded.setText("text");
embedded.setNumber(42L);
embedded.setFlag(true);
parentType.setEmbedded(embedded);
Datastore ds = getDs();
ds.save(parentType);
Query<ParentType> query = ds.find(ParentType.class)
.disableValidation()
.field("embedded.flag").equal(true);
Assert.assertEquals(parentType, query.get());
}
示例4: update
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Override
public void update(List<Provider> entityList) throws DBException {
for (Provider provider : entityList) {
List<Provider> providers = fetchById(new ArrayList<String>() {{
add(provider.getCloudProvider());
}});
Provider existingProvider = null;
if (providers != null && !providers.isEmpty()) {
existingProvider = providers.get(0);
}
if (existingProvider != null) {
UpdateOperations<Provider> ops = this.createUpdateOperations();
if (provider.getCommands() != null) {
ops.set("commands", provider.getCommands());
}
if (provider.getDockerImage() != null) {
ops.set("dockerImage", provider.getDockerImage());
}
Query<Provider> updateQuery = this.createQuery().field(Mapper.ID_KEY).equal(provider.getCloudProvider());
UpdateResults results = this.update(updateQuery, ops);
}
}
}
示例5: startBundle
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Override
public boolean startBundle(long bundleId, String processorId, ObjectId startTx, String branch, String trunk, long lastTransactionIndex, long nextAddressIndex) {
//Let's make sure a bundle is created with this bundleId first, because in the next query we are putting some more restrictions, and we don't want to upsert these
datastore.update(datastore.createQuery(StoredBundle.class).field("bundleId").equal(bundleId), datastore.createUpdateOperations(StoredBundle.class).set("bundleId", bundleId), new UpdateOptions().upsert(true));
//TODO: Started time should be the last transaction of the previous bundle, lastTransactionTime
Query<StoredBundle> query = datastore.createQuery(StoredBundle.class).field("bundleId").equal(bundleId).field("started").doesNotExist().field("processorId").doesNotExist();
UpdateOperations<StoredBundle> updateOperations = datastore.createUpdateOperations(StoredBundle.class)
.set("started", System.currentTimeMillis())
.set("currentTransaction", startTx)
.set("branch", branch)
.set("trunk", trunk)
.set("processorId", processorId)
.set("lastTransactionIndex", lastTransactionIndex)
.set("nextAddressIndex", nextAddressIndex);
return datastore.update(query, updateOperations).getUpdatedCount() > 0;
}
示例6: getJoinableServer
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Override
public Server getJoinableServer(Filter filter, String uuid) {
Query<Server> query = getFilterQuery(filter);
query.and(getServerQuery().criteria("joinable").equal(true),
getServerQuery().criteria("expiry").greaterThan(System.currentTimeMillis()),
getServerQuery()
.or(new WhereCriteria("this.mpc > this.pc"),
getServerQuery().criteria("mpc").equal(0)));
query.limit(1);
query.order("pc");
query.getSortObject().put("pc", -1);
UpdateOperations<Server> updateOperations = datastore.createUpdateOperations(Server.class);
updateOperations.inc("pc", 1);
updateOperations.add("players", uuid);
Server server = datastore.findAndModify(query, updateOperations, false, false);
if (server == null)
return null;
return server;
}
示例7: enrichQuery
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
private void enrichQuery(Query<?> mongoQuery, String field, Collection<Object> criteria) {
if (criteria.size() == 1) {
// range query
Object val = criteria.iterator().next();
// TODO: rather operatte on ParsedField and have a method to determine whether it's some operation
if (field.contains(">") || field.contains("<")) {
mongoQuery.filter(field, val);
} else if (val instanceof ServiceQueryReservedValue) {
adaptToReservedValue(mongoQuery, field, (ServiceQueryReservedValue) val);
} else {
mongoQuery.field(field).equal(val);
}
} else {
// TODO: deal nicely with other operations
if (field.contains("<>")) {
mongoQuery.field(ServiceQueryUtil.parseQueryField(field).getFieldName()).notIn(criteria);
} else {
mongoQuery.field(field).in(criteria);
}
}
}
示例8: getTotalItemsCnt
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
private Long getTotalItemsCnt(Query<V> q, ServiceQuery<?> serviceQuery, List<V> results) {
if (!serviceQuery.isCountTotalItems()) {
return null;
}
if (results.size() > q.getLimit()) {
throw new RestDslException("Implementation error: results size must be not greater than limit, was " +
results.size() + " but limit was: " + q.getLimit());
}
if (serviceQuery.getCountOnly()) {
return q.countAll();
}
// if getLimit == 0 then we need to count anyway
if (results.size() == 0 && q.getOffset() == 0 && q.getLimit() > 0) {
return 0L;
}
// if size is equal 0 it could be that offset is too big, or we just have 0 elements in total - must count
if (results.size() != 0 && results.size() < q.getLimit()) {
return (long) (q.getOffset() + results.size());
} else {
return q.countAll();
}
}
示例9: selectiveUpdate
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Override
public boolean selectiveUpdate(JobQueueReq request) {
if (StringUtils.isEmpty(request.getJobId())) {
throw new JdbcException("Only allow by jobId");
}
Query<JobPo> query = template.createQuery(getTargetTable(request.getTaskTrackerNodeGroup()), JobPo.class);
query.field("jobId").equal(request.getJobId());
UpdateOperations<JobPo> operations = template.createUpdateOperations(JobPo.class);
addUpdateField(operations, "cronExpression", request.getCronExpression());
addUpdateField(operations, "needFeedback", request.getNeedFeedback());
addUpdateField(operations, "extParams", request.getExtParams());
addUpdateField(operations, "triggerTime", request.getTriggerTime() == null ? null : request.getTriggerTime().getTime());
addUpdateField(operations, "priority", request.getPriority());
addUpdateField(operations, "maxRetryTimes", request.getMaxRetryTimes());
addUpdateField(operations, "submitNodeGroup", request.getSubmitNodeGroup());
addUpdateField(operations, "taskTrackerNodeGroup", request.getTaskTrackerNodeGroup());
UpdateResults ur = template.update(query, operations);
return ur.getUpdatedCount() == 1;
}
示例10: lockJob
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
protected boolean lockJob(String taskTrackerNodeGroup, String jobId, String taskTrackerIdentity, Long triggerTime, Long gmtModified) {
UpdateOperations<JobPo> operations =
template.createUpdateOperations(JobPo.class)
.set("isRunning", true)
.set("taskTrackerIdentity", taskTrackerIdentity)
.set("gmtModified", SystemClock.now());
String tableName = JobQueueUtils.getExecutableQueueName(taskTrackerNodeGroup);
Query<JobPo> updateQuery = template.createQuery(tableName, JobPo.class);
updateQuery.field("jobId").equal(jobId)
.field("isRunning").equal(false)
.field("triggerTime").equal(triggerTime)
.field("gmtModified").equal(gmtModified);
UpdateResults updateResult = template.update(updateQuery, operations);
return updateResult.getUpdatedCount() == 1;
}
示例11: filter
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
/**
*
* @param queryCriteria - key is field and operator and value is RHS value
* eg: "Location in" => "Delhi"
* @param sortCriteria - comma separated field names. use - for descending
* order eg: "location, -status"
* @param pageInfo - contain limit and offset
* @return
*/
public List<T> filter(Map<String, Object> queryCriteria, String sortCriteria, PageInfo pageInfo) {
Query<T> query = datastore.createQuery(genericType);
for (Map.Entry<String, Object> entry : queryCriteria.entrySet()) {
query = query.filter(entry.getKey(), entry.getValue());
}
if (pageInfo == null) {
pageInfo = new PageInfo(100000, 0);
}
if (sortCriteria == null) {
sortCriteria = "_id";
}
query = query.limit(pageInfo.getLimit()).offset(pageInfo.getOffset());
query = query.order(sortCriteria);
return query.asList();
}
示例12: testRetrieveMovies
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Test
public void testRetrieveMovies()
{
Query<Movie> qMovies = datastore.createQuery(Movie.class);
Assert.assertEquals(158, qMovies.count());
for (Movie movie : qMovies)
{
Set<ConstraintViolation<Movie>> violations = validator.validate(movie);
Assert.assertEquals(0, violations.size());
//??// testCollection(movie.getRating(), Rating.class);
testCollection(movie.getCriticisms(), Criticism.class);
testCollection(movie.getPrizes(), Prize.class);
}
}
示例13: testRetrieveSanctions
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Test
public void testRetrieveSanctions()
{
Query<Sanctions> qSanctions = newDatastore.createQuery(Sanctions.class);
Assert.assertEquals(32040, qSanctions.count());
for (Sanctions sanction : qSanctions)
{
Set<ConstraintViolation<Sanctions>> violations = validator.validate(sanction);
Assert.assertEquals(0, violations.size());
testCollection(sanction.getBirth_dates(), Birth_date.class);
testCollection(sanction.getNationalities(), Nationality.class);
testCollection(sanction.getAliases(), Alias.class);
testCollection(sanction.getIdentifiers(), Identifier.class);
testCollection(sanction.getBirth_places(), Birth_place.class);
testCollection(sanction.getAddresses(), Address.class);
}
}
示例14: testRetrieveOrganizations
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Test
public void testRetrieveOrganizations()
{
// We actually detected here a bug. When an aggregate/reference cardinality is 0..1, it may actually be an array.
Query<Organizations> qOrganizations = datastore.createQuery(Organizations.class);
Assert.assertEquals(11, qOrganizations.count());
for (Organizations organization : qOrganizations)
{
Set<ConstraintViolation<Organizations>> violations = validator.validate(organization);
Assert.assertEquals(0, violations.size());
testCollection(organization.getOther_names(), Other_name.class);
testCollection(organization.getIdentifiers(), Identifier.class);
testCollection(organization.getLinks(), Link.class);
}
}
示例15: testRetrieveEvents
import org.mongodb.morphia.query.Query; //导入依赖的package包/类
@Test
public void testRetrieveEvents()
{
Query<Events> qEvents = datastore.createQuery(Events.class);
Assert.assertEquals(62, qEvents.count());
for (Events event : qEvents)
{
Set<ConstraintViolation<Events>> violations = validator.validate(event);
Assert.assertEquals(0, violations.size());
testCollection(event.getIdentifiers(), Identifier.class);
// Dont have to check if the Organization is valid, since we just validated it on the previous method...
if (event.getOrganization_id() != null)
Assert.assertEquals(1, datastore.createQuery(Organizations.class).filter("_id =", event.getOrganization_id().get_id()).count());
}
}