Search in sources :

Example 1 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method testUpdateShouldAllowMultiplePushAll.

// DATAMONGO-354, DATAMONGO-1824
@Test
@MongoVersion(until = "3.6")
@SuppressWarnings("deprecation")
public void testUpdateShouldAllowMultiplePushAll() {
    DocumentWithMultipleCollections doc = new DocumentWithMultipleCollections();
    doc.id = "1234";
    doc.string1 = Arrays.asList("spring");
    doc.string2 = Arrays.asList("one");
    template.save(doc);
    Update update = new Update().pushAll("string1", new Object[] { "data", "mongodb" });
    update.pushAll("string2", new String[] { "two", "three" });
    Query findQuery = new Query(Criteria.where("id").is(doc.id));
    template.updateFirst(findQuery, update, DocumentWithMultipleCollections.class);
    DocumentWithMultipleCollections result = template.findOne(findQuery, DocumentWithMultipleCollections.class);
    assertThat(result.string1, hasItems("spring", "data", "mongodb"));
    assertThat(result.string2, hasItems("one", "two", "three"));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 2 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method updateMultiShouldAddValuesCorrectlyWhenUsingPushEachWithSimpleTypes.

// DATAMONGO-812
@Test
@MongoVersion(asOf = "2.4")
public void updateMultiShouldAddValuesCorrectlyWhenUsingPushEachWithSimpleTypes() {
    DocumentWithCollectionOfSimpleType document = new DocumentWithCollectionOfSimpleType();
    document.values = Arrays.asList("spring");
    template.save(document);
    Query query = query(where("id").is(document.id));
    assumeThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values, hasSize(1));
    Update update = new Update().push("values").each("data", "mongodb");
    template.updateMulti(query, update, DocumentWithCollectionOfSimpleType.class);
    assertThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values, hasSize(3));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 3 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldPerformStringProjectionOperatorsCorrectly.

// DATAMONGO-774
@Test
@MongoVersion(asOf = "2.4")
public void shouldPerformStringProjectionOperatorsCorrectly() throws ParseException {
    Data data = new Data();
    data.dateValue = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSSZ").parse("29.08.1983 12:34:56.789+0000");
    mongoTemplate.insert(data);
    TypedAggregation<Data> agg = newAggregation(Data.class, // 
    project().andExpression("dayOfYear(dateValue)").as(// 
    "dayOfYear").andExpression("dayOfMonth(dateValue)").as(// 
    "dayOfMonth").andExpression("dayOfWeek(dateValue)").as(// 
    "dayOfWeek").andExpression("year(dateValue)").as(// 
    "year").andExpression("month(dateValue)").as(// 
    "month").andExpression("week(dateValue)").as(// 
    "week").andExpression("hour(dateValue)").as(// 
    "hour").andExpression("minute(dateValue)").as(// 
    "minute").andExpression("second(dateValue)").as(// 
    "second").andExpression("millisecond(dateValue)").as(// 
    "millisecond"));
    AggregationResults<Document> results = mongoTemplate.aggregate(agg, Document.class);
    Document document = results.getUniqueMappedResult();
    assertThat(document, is(notNullValue()));
    assertThat((Integer) document.get("dayOfYear"), is(241));
    assertThat((Integer) document.get("dayOfMonth"), is(29));
    assertThat((Integer) document.get("dayOfWeek"), is(2));
    assertThat((Integer) document.get("year"), is(1983));
    assertThat((Integer) document.get("month"), is(8));
    assertThat((Integer) document.get("week"), is(35));
    assertThat((Integer) document.get("hour"), is(12));
    assertThat((Integer) document.get("minute"), is(34));
    assertThat((Integer) document.get("second"), is(56));
    assertThat((Integer) document.get("millisecond"), is(789));
}
Also used : Document(org.bson.Document) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 4 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldPerformDateProjectionOperatorsCorrectly.

// DATAMONGO-774
@Test
@MongoVersion(asOf = "2.4")
public void shouldPerformDateProjectionOperatorsCorrectly() throws ParseException {
    Data data = new Data();
    data.stringValue = "ABC";
    mongoTemplate.insert(data);
    TypedAggregation<Data> agg = newAggregation(Data.class, // 
    project().andExpression("concat(stringValue, 'DE')").as(// 
    "concat").andExpression("strcasecmp(stringValue,'XYZ')").as(// 
    "strcasecmp").andExpression("substr(stringValue,1,1)").as(// 
    "substr").andExpression("toLower(stringValue)").as(// 
    "toLower").andExpression("toUpper(toLower(stringValue))").as(// 
    "toUpper"));
    AggregationResults<Document> results = mongoTemplate.aggregate(agg, Document.class);
    Document document = results.getUniqueMappedResult();
    assertThat(document, is(notNullValue()));
    assertThat((String) document.get("concat"), is("ABCDE"));
    assertThat((Integer) document.get("strcasecmp"), is(-1));
    assertThat((String) document.get("substr"), is("B"));
    assertThat((String) document.get("toLower"), is("abc"));
    assertThat((String) document.get("toUpper"), is("ABC"));
}
Also used : Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 5 with MongoVersion

use of org.springframework.data.mongodb.test.util.MongoVersion in project spring-data-mongodb by spring-projects.

the class AggregationTests method letShouldBeAppliedCorrectly.

// DATAMONGO-1538
@Test
@MongoVersion(asOf = "3.2")
public void letShouldBeAppliedCorrectly() {
    Sales2 sales1 = Sales2.builder().id("1").price(10).tax(0.5F).applyDiscount(true).build();
    Sales2 sales2 = Sales2.builder().id("2").price(10).tax(0.25F).applyDiscount(false).build();
    mongoTemplate.insert(Arrays.asList(sales1, sales2), Sales2.class);
    ExpressionVariable total = ExpressionVariable.newVariable("total").forExpression(AggregationFunctionExpressions.ADD.of(Fields.field("price"), Fields.field("tax")));
    ExpressionVariable discounted = ExpressionVariable.newVariable("discounted").forExpression(ConditionalOperators.Cond.when("applyDiscount").then(0.9D).otherwise(1.0D));
    TypedAggregation<Sales2> agg = Aggregation.newAggregation(Sales2.class, Aggregation.project().and(VariableOperators.Let.define(total, discounted).andApply(AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted")))).as("finalTotal"));
    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
    assertThat(result.getMappedResults(), contains(new Document("_id", "1").append("finalTotal", 9.450000000000001D), new Document("_id", "2").append("finalTotal", 10.25D)));
}
Also used : ExpressionVariable(org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable) Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Aggregations

Test (org.junit.Test)19 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)19 Document (org.bson.Document)16 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)3 Query (org.springframework.data.mongodb.core.query.Query)3 Update (org.springframework.data.mongodb.core.query.Update)3 IsBsonObject (org.springframework.data.mongodb.test.util.IsBsonObject)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ExpressionVariable (org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable)1 Person (org.springframework.data.mongodb.repository.Person)1