Java Code Examples for org.springframework.data.mongodb.core.query.Query#getFieldsObject()

The following examples show how to use org.springframework.data.mongodb.core.query.Query#getFieldsObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoEntityRepository.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private Query addEmbededFields(Query query, Set<String> embededFields) {
    if (query == null) {
        return null;
    }
    DBObject fieldObjects = query.getFieldsObject();
    if (fieldObjects != null) {
        for (String embededField : embededFields) {
            if (!fieldObjects.containsField(embededField)) {
                fieldObjects.put(embededField, 1);
            }
        }
    }
    return query;
}
 
Example 2
Source File: MongoEntityTemplate.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public <T> Iterator<T> findEach(final Query query, final Class<T> entityClass, String collectionName) {
   MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);

   DBCollection collection = getDb().getCollection(collectionName);

   prepareCollection(collection);

   DBObject dbQuery = mapper.getMappedObject(query.getQueryObject(), entity);
   final DBCursor cursor;

   if (query.getFieldsObject() == null) {
       cursor = collection.find(dbQuery);
   } else {
       cursor = collection.find(dbQuery, query.getFieldsObject());
   }

   return new Iterator<T>() {
       @Override
       public boolean hasNext() {
           // TODO Auto-generated method stub
           return cursor.hasNext();
       }

       @Override
       public T next() {
           return getConverter().read(entityClass, cursor.next());
       }

       @Override
       public void remove() {
           cursor.remove();
       }
   };
}
 
Example 3
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeFieldConvert() {
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.setIncludeFieldString("populationServed,uniqueSectionCode");

    Query query = mongoQueryConverter.convert("section", neutralQuery);
    assertNotNull("Should not be null", query);
    DBObject obj = query.getFieldsObject();
    assertNotNull("Should not be null", obj);
    assertEquals("Should match", 1, obj.get("body.populationServed"));
    assertEquals("Should match", 1, obj.get("body.uniqueSectionCode"));
}
 
Example 4
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyIncludeFieldConvert() {
    NeutralQuery neutralQuery = new NeutralQuery();

    Query query = mongoQueryConverter.convert("section", neutralQuery);
    assertNotNull("Should not be null", query);
    DBObject obj = query.getFieldsObject();
    assertNotNull("Should not be null", obj);
    assertEquals("Should match", 1, obj.get("body"));
    assertEquals("Should match", 1, obj.get("type"));
    assertEquals("Should match", 1, obj.get("metaData"));
}
 
Example 5
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmbeddedFieldConvert() {
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.setEmbeddedFields(Arrays.asList("studentSectionAssociation", "teacherSectionAssociation"));

    Query query = mongoQueryConverter.convert("section", neutralQuery);
    assertNotNull("Should not be null", query);
    DBObject obj = query.getFieldsObject();
    assertNotNull("Should not be null", obj);
    assertEquals("Should match", 1, obj.get("studentSectionAssociation"));
    assertEquals("Should match", 1, obj.get("teacherSectionAssociation"));
    assertEquals("Should match", 1, obj.get("body"));
    assertEquals("Should match", 1, obj.get("type"));
    assertEquals("Should match", 1, obj.get("metaData"));
}
 
Example 6
Source File: MongoQueryConverterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonEmptyIncludeFieldConvert() {
    NeutralQuery neutralQuery = new NeutralQuery();
    neutralQuery.setIncludeFieldString("name");

    Query query = mongoQueryConverter.convert("section", neutralQuery);
    assertNotNull("Should not be null", query);
    DBObject obj = query.getFieldsObject();
    assertNotNull("Should not be null", obj);
    assertEquals("Should match", 1, obj.get("body.name"));
    assertEquals("Should match", 1, obj.get("type"));
    assertEquals("Should match", 1, obj.get("metaData"));
    assertNull("Should be null", obj.get("body"));
}