Example usage for org.springframework.data.mongodb.core.query Query getSortObject

List of usage examples for org.springframework.data.mongodb.core.query Query getSortObject

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query Query getSortObject.

Prototype

public Document getSortObject() 

Source Link

Usage

From source file:com.trenako.repositories.mongo.RollingStockQueryBuilderTests.java

private void assertSort(Query query, String sortText) {
    assertEquals(sortText, query.getSortObject().toString());
}

From source file:com.trenako.repositories.mongo.BrowseRepositoryTests.java

@Test
public void shouldGetBrands() {
    List<Brand> value = Arrays.asList(acme(), roco());

    ArgumentCaptor<Query> arg = ArgumentCaptor.forClass(Query.class);
    when(mongo.find(isA(Query.class), eq(Brand.class))).thenReturn(value);

    Iterable<Brand> brands = repo.getBrands();

    assertNotNull(brands);//from  w w w .  ja  va 2s. c om
    verify(mongo, times(1)).find(arg.capture(), eq(Brand.class));
    Query q = arg.getValue();
    assertEquals("{ \"name\" : 1}", q.getSortObject().toString());
}

From source file:com.trenako.repositories.mongo.BrowseRepositoryTests.java

@Test
public void shouldGetScales() {
    List<Scale> value = Arrays.asList(scaleH0(), scaleN());

    ArgumentCaptor<Query> arg = ArgumentCaptor.forClass(Query.class);
    when(mongo.find(isA(Query.class), eq(Scale.class))).thenReturn(value);

    Iterable<Scale> scales = repo.getScales();

    assertNotNull(scales);//from   ww  w. j a v a2 s  .co  m
    verify(mongo, times(1)).find(arg.capture(), eq(Scale.class));
    Query q = arg.getValue();
    assertEquals("{ \"name\" : 1}", q.getSortObject().toString());
}

From source file:com.trenako.repositories.mongo.BrowseRepositoryTests.java

@Test
public void shouldGetRailways() {
    List<Railway> value = Arrays.asList(fs(), db());

    ArgumentCaptor<Query> arg = ArgumentCaptor.forClass(Query.class);
    when(mongo.find(isA(Query.class), eq(Railway.class))).thenReturn(value);

    Iterable<Railway> railways = repo.getRailways();

    assertNotNull(railways);//from  ww w.j  av  a 2 s. co  m
    verify(mongo, times(1)).find(arg.capture(), eq(Railway.class));
    Query q = arg.getValue();
    assertEquals("{ \"name\" : 1}", q.getSortObject().toString());
}

From source file:com.skymobi.monitor.model.LogQueryTest.java

public void test_query() throws Exception {

    assertEquals("{ }", query.toQuery().toString());
    query.setStart("2012-06-08 10:00:00");

    assertEquals("{ \"timestamp\" : { \"$gt\" : { \"$date\" : \"2012-06-08T02:00:00.000Z\"}}}",
            query.toQuery().toString());
    query.setEnd("2012-06-08 11:00:00");
    query.setLevel("ERROR");
    query.setKeyWord("111");
    System.out.println(query.toQuery().toString());

    Query bquery = new BasicQuery(query.toQuery());
    bquery.limit(100);/*from  ww  w.j av  a2 s  .  com*/

    bquery.sort().on("$timestamp", Order.DESCENDING);
    System.out.println(bquery.getQueryObject());
    System.out.println(bquery.getFieldsObject());
    System.out.println(bquery.getSortObject());
    //        assertEquals("{ \"timestamp\" : { \"$gt\" : { \"$date\" : \"1970-01-01T00:01:40.000Z\"}} , \"$where\" : \"this.message && this.message.match('hello')\"}",query.toQuery().toString());
}

From source file:com.skymobi.monitor.service.LogsService.java

public DBCursor findLogs(String projectName, LogQuery logQuery, int max) throws ParseException {
    Project project = projectService.findProject(projectName);
    MongoTemplate template = project.fetchMongoTemplate();

    Query query = new BasicQuery(logQuery.toQuery());
    query.limit(max);//  ww w.  j a  va 2  s.  c  om

    query.sort().on("timestamp", Order.DESCENDING);
    logger.debug("find logs from {}  by query {} by sort {}",
            new Object[] { project.getLogCollection(), query.getQueryObject(), query.getSortObject() });
    DBCursor cursor = template.getCollection(project.getLogCollection()).find(query.getQueryObject())
            .sort(query.getSortObject()).limit(max);
    return cursor;
}

From source file:com.appleframework.monitor.model.LogQueryTest.java

public void test_query() throws Exception {

    assertEquals("{ }", query.toQuery().toString());
    query.setStart("2012-06-08 10:00:00");

    assertEquals("{ \"timestamp\" : { \"$gt\" : { \"$date\" : \"2012-06-08T02:00:00.000Z\"}}}",
            query.toQuery().toString());
    query.setEnd("2012-06-08 11:00:00");
    query.setLevel("ERROR");
    query.setKeyWord("111");
    System.out.println(query.toQuery().toString());

    Query bquery = new BasicQuery(query.toQuery());
    bquery.limit(100);//w w w. j  a va 2s. c o m

    //bquery.sort().on("$timestamp", Order.DESCENDING);
    bquery.with(new Sort(Direction.DESC, "$timestamp"));
    System.out.println(bquery.getQueryObject());
    System.out.println(bquery.getFieldsObject());
    System.out.println(bquery.getSortObject());
    //        assertEquals("{ \"timestamp\" : { \"$gt\" : { \"$date\" : \"1970-01-01T00:01:40.000Z\"}} , \"$where\" : \"this.message && this.message.match('hello')\"}",query.toQuery().toString());
}

From source file:com.appleframework.monitor.service.LogsService.java

public DBCursor findLogs(String projectName, LogQuery logQuery, int max) throws ParseException {
    Project project = projectService.findProject(projectName);
    MongoTemplate template = project.fetchMongoTemplate();

    Query query = new BasicQuery(logQuery.toQuery());
    query.limit(max);/*from  ww w.j a va2  s  . co  m*/

    //query.sort().on("timestamp", Order.DESCENDING);
    query.with(new Sort(Direction.DESC, "timestamp"));
    logger.debug("find logs from {}  by query {} by sort {}",
            new Object[] { project.getLogCollection(), query.getQueryObject(), query.getSortObject() });
    DBCursor cursor = template.getCollection(project.getLogCollection()).find(query.getQueryObject())
            .sort(query.getSortObject()).limit(max);
    return cursor;
}

From source file:org.tylproject.vaadin.addon.MongoContainer.java

/**
 * @return a cursor with the given optional params
 *///from w  w  w  .  j  ava 2s  .c  o  m
protected DBCursor cursor(DBObject additionalCriteria) {
    final Query q = this.query;
    DBObject criteriaObject = q.getQueryObject();
    if (additionalCriteria != null) {
        criteriaObject.putAll(additionalCriteria);
    }

    DBObject projectionObject = new BasicDBObject(ID, true);

    String collectionName = mongoOps.getCollectionName(beanClass);
    DBCollection dbCollection = mongoOps.getCollection(collectionName);

    // TODO: keep cursor around to possibly reuse
    DBCursor cursor = dbCollection.find(criteriaObject, projectionObject);

    if (this.baseSort != null || this.sort != null) {
        DBObject sortObject = q.getSortObject();
        cursor.sort(sortObject);
    }

    return cursor;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Execute a MongoDB query and iterate over the query results on a per-document basis with a
 * {@link DocumentCallbackHandler} using the provided CursorPreparer.
 * /*from  www  . j a va  2s .c o m*/
 * @param query the query class that specifies the criteria used to find a record and also an optional fields
 *          specification, must not be {@literal null}.
 * @param collectionName name of the collection to retrieve the objects from
 * @param dch the handler that will extract results, one document at a time
 * @param preparer allows for customization of the {@link DBCursor} used when iterating over the result set, (apply
 *          limits, skips and so on).
 */
protected void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch,
        CursorPreparer preparer) {

    Assert.notNull(query);

    DBObject queryObject = query.getQueryObject();
    DBObject sortObject = query.getSortObject();
    DBObject fieldsObject = query.getFieldsObject();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: $s",
                serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName));
    }

    this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
}