Search in sources :

Example 46 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method doesNotTranslateNonIdPropertiesFor$NeCriteria.

// DATAMONGO-493
@Test
void doesNotTranslateNonIdPropertiesFor$NeCriteria() {
    ObjectId accidentallyAnObjectId = new ObjectId();
    Query query = Query.query(Criteria.where("id").is("id_value").and("publishers").ne(accidentallyAnObjectId.toString()));
    org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(UserEntity.class));
    assertThat(document.get("publishers")).isInstanceOf(org.bson.Document.class);
    org.bson.Document publishers = (org.bson.Document) document.get("publishers");
    assertThat(publishers).containsKey("$ne");
    assertThat(publishers.get("$ne")).isInstanceOf(String.class);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) ObjectId(org.bson.types.ObjectId) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 47 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method mapsNullBsonTypeForFieldWithCustomTargetType.

// GH-3633
@Test
void mapsNullBsonTypeForFieldWithCustomTargetType() {
    Query query = query(where("stringAsOid").isNullValue());
    org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
    assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", new org.bson.Document("$type", 10)));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 48 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method getMappedSortIgnoresTextScoreWhenNotSortedByScore.

// DATAMONGO-973
@Test
void getMappedSortIgnoresTextScoreWhenNotSortedByScore() {
    Query query = new Query().with(Sort.by("id"));
    org.bson.Document document = mapper.getMappedSort(query.getSortObject(), context.getPersistentEntity(WithTextScoreProperty.class));
    assertThat(document).isEqualTo(new org.bson.Document().append("_id", 1));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 49 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method getMappedFieldsReplacesTextScoreFieldProperlyCorrectlyWhenPresent.

// DATAMONGO-973
@Test
void getMappedFieldsReplacesTextScoreFieldProperlyCorrectlyWhenPresent() {
    Query query = new Query();
    query.fields().include("textScore");
    org.bson.Document document = mapper.getMappedFields(query.getFieldsObject(), context.getPersistentEntity(WithTextScoreProperty.class));
    assertThat(document).isEqualTo(new org.bson.Document().append("score", new org.bson.Document("$meta", "textScore")));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 50 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames.

// DATAMONGO-1406
@Test
void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() {
    EmbeddedClass embeddedClass = new EmbeddedClass();
    embeddedClass.customizedField = "hello";
    Foo foo = new Foo();
    foo.listOfItems = Collections.singletonList(embeddedClass);
    Query query = new Query(// 
    Criteria.where("listOfItems").elemMatch(// 
    new Criteria().andOperator(Criteria.where("customizedField").is(embeddedClass.customizedField))));
    org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
    assertThat(document).containsEntry("my_items.$elemMatch.$and", Collections.singletonList(new org.bson.Document("fancy_custom_name", embeddedClass.customizedField)));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) Criteria(org.springframework.data.mongodb.core.query.Criteria) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.springframework.data.mongodb.core.mapping.Document)55 Test (org.junit.jupiter.api.Test)53 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)16 Query (org.springframework.data.mongodb.core.query.Query)16 TextQuery (org.springframework.data.mongodb.core.query.TextQuery)16 Point (org.springframework.data.geo.Point)12 BasicDBList (com.mongodb.BasicDBList)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 DBRef (com.mongodb.DBRef)5 Sphere (org.springframework.data.mongodb.core.geo.Sphere)5 ObjectId (org.bson.types.ObjectId)4 Distance (org.springframework.data.geo.Distance)4 LocalDate (org.joda.time.LocalDate)3 MongoException (com.mongodb.MongoException)2 BigInteger (java.math.BigInteger)2 HashSet (java.util.HashSet)2 DataAccessException (org.springframework.dao.DataAccessException)2 Circle (org.springframework.data.geo.Circle)2 Polygon (org.springframework.data.geo.Polygon)2