Search in sources :

Example 51 with Document

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

the class QueryMapperUnitTests method handlesObjectIdCapableBigIntegerIdsCorrectly.

@Test
void handlesObjectIdCapableBigIntegerIdsCorrectly() {
    ObjectId id = new ObjectId();
    org.bson.Document document = new org.bson.Document("id", new BigInteger(id.toString(), 16));
    org.bson.Document result = mapper.getMappedObject(document, context.getPersistentEntity(IdWrapper.class));
    assertThat(result).containsEntry("_id", id);
}
Also used : ObjectId(org.bson.types.ObjectId) BigInteger(java.math.BigInteger) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 52 with Document

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

the class QueryMapperUnitTests method getMappedSortAppendsTextScoreProperlyWhenSortedByScore.

// DATAMONGO-973
@Test
void getMappedSortAppendsTextScoreProperlyWhenSortedByScore() {
    Query query = new Query().with(Sort.by("textScore"));
    org.bson.Document document = mapper.getMappedSort(query.getSortObject(), 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 53 with Document

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

the class QueryMapperUnitTests method doesHandleNestedFieldsWithDefaultIdNames.

@Test
void doesHandleNestedFieldsWithDefaultIdNames() {
    org.bson.Document document = new org.bson.Document("id", new ObjectId().toString());
    document.put("nested", new org.bson.Document("id", new ObjectId().toString()));
    MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(ClassWithDefaultId.class);
    org.bson.Document result = mapper.getMappedObject(document, entity);
    assertThat(result.get("_id")).isInstanceOf(ObjectId.class);
    assertThat(((org.bson.Document) result.get("nested")).get("_id")).isInstanceOf(ObjectId.class);
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 54 with Document

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

the class GeoJsonTests method shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger.

// DATAMONGO-1453
@Test
public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
    this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class), new CollectionCallback<Object>() {

        @Override
        public Object doInCollection(MongoCollection<org.bson.Document> collection) throws MongoException, DataAccessException {
            org.bson.Document lineStringRepresentation = new org.bson.Document();
            lineStringRepresentation.put("type", "LineString");
            lineStringRepresentation.put("coordinates", new BasicDbListBuilder().add(new BasicDbListBuilder().add(0).add(0).get()).add(new BasicDbListBuilder().add(1).add(1).get()).get());
            org.bson.Document document = new org.bson.Document();
            document.append("_id", "datamongo-1453");
            document.append("geoJsonLineString", lineStringRepresentation);
            collection.insertOne(document);
            return document;
        }
    });
    assertThat(template.findOne(query(where("id").is("datamongo-1453")), DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString).isEqualTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1)));
}
Also used : MongoException(com.mongodb.MongoException) BasicDbListBuilder(org.springframework.data.mongodb.test.util.BasicDbListBuilder) Point(org.springframework.data.geo.Point) Document(org.springframework.data.mongodb.core.mapping.Document) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 55 with Document

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

the class MongoPersistentEntityIndexResolver method resolveIndexForEntity.

/**
 * Resolve the {@link IndexDefinition}s for a given {@literal root} entity by traversing
 * {@link MongoPersistentProperty} scanning for index annotations {@link Indexed}, {@link CompoundIndex} and
 * {@link GeospatialIndex}. The given {@literal root} has therefore to be annotated with {@link Document}.
 *
 * @param root must not be null.
 * @return List of {@link IndexDefinitionHolder}. Will never be {@code null}.
 * @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities.
 */
public List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<?> root) {
    Assert.notNull(root, "MongoPersistentEntity must not be null!");
    Document document = root.findAnnotation(Document.class);
    Assert.notNull(document, () -> String.format("Entity %s is not a collection root. Make sure to annotate it with @Document!", root.getName()));
    verifyWildcardIndexedProjection(root);
    List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
    String collection = root.getCollection();
    indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", collection, root));
    indexInformation.addAll(potentiallyCreateWildcardIndexDefinitions("", collection, root));
    indexInformation.addAll(potentiallyCreateTextIndexDefinition(root, collection));
    root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this.potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard()));
    indexInformation.addAll(resolveIndexesForDbrefs("", collection, root));
    return indexInformation;
}
Also used : EvaluationContextProvider(org.springframework.data.spel.EvaluationContextProvider) Arrays(java.util.Arrays) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Association(org.springframework.data.mapping.Association) BsonUtils(org.springframework.data.mongodb.util.BsonUtils) ParserContext(org.springframework.expression.ParserContext) IncludeStrategy(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.TextIndexIncludeOptions.IncludeStrategy) LiteralExpression(org.springframework.expression.common.LiteralExpression) MappingContext(org.springframework.data.mapping.context.MappingContext) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) TypeInformation(org.springframework.data.util.TypeInformation) Collation(org.springframework.data.mongodb.core.query.Collation) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) Document(org.springframework.data.mongodb.core.mapping.Document) HashSet(java.util.HashSet) MappingException(org.springframework.data.mapping.MappingException) Duration(java.time.Duration) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) PersistentProperty(org.springframework.data.mapping.PersistentProperty) TextIndexedFieldSpec(org.springframework.data.mongodb.core.index.TextIndexDefinition.TextIndexedFieldSpec) ClassUtils(org.springframework.util.ClassUtils) Iterator(java.util.Iterator) Path(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.CycleGuard.Path) Collection(java.util.Collection) ObjectUtils(org.springframework.util.ObjectUtils) Set(java.util.Set) Collectors(java.util.stream.Collectors) PersistentEntity(org.springframework.data.mapping.PersistentEntity) EvaluationContext(org.springframework.expression.EvaluationContext) TimeUnit(java.util.concurrent.TimeUnit) AssociationHandler(org.springframework.data.mapping.AssociationHandler) List(java.util.List) BasicMongoPersistentEntity(org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity) DotPath(org.springframework.data.mongodb.util.DotPath) Expression(org.springframework.expression.Expression) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) TextIndexDefinitionBuilder(org.springframework.data.mongodb.core.index.TextIndexDefinition.TextIndexDefinitionBuilder) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document)

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