Search in sources :

Example 16 with Document

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

the class QueryMapperUnitTests method translates$NeCorrectly.

// DATAMONGO-278
@Test
void translates$NeCorrectly() {
    Criteria criteria = where("foo").ne(new ObjectId().toString());
    org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(), context.getPersistentEntity(Sample.class));
    Object object = result.get("_id");
    assertThat(object).isInstanceOf(org.bson.Document.class);
    org.bson.Document document = (org.bson.Document) object;
    assertThat(document.get("$ne")).isInstanceOf(ObjectId.class);
}
Also used : ObjectId(org.bson.types.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) Criteria(org.springframework.data.mongodb.core.query.Criteria) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 17 with Document

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

the class GeoJsonTests method shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger.

// DATAMONGO-1453
@Test
public void shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
    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 pointRepresentation = new org.bson.Document();
            pointRepresentation.put("type", "Point");
            pointRepresentation.put("coordinates", new BasicDbListBuilder().add(0).add(0).get());
            org.bson.Document document = new org.bson.Document();
            document.append("_id", "datamongo-1453");
            document.append("geoJsonPoint", pointRepresentation);
            collection.insertOne(document);
            return document;
        }
    });
    assertThat(template.findOne(query(where("id").is("datamongo-1453")), DocumentWithPropertyUsingGeoJsonType.class).geoJsonPoint).isEqualTo(new GeoJsonPoint(0D, 0D));
}
Also used : MongoException(com.mongodb.MongoException) BasicDbListBuilder(org.springframework.data.mongodb.test.util.BasicDbListBuilder) Document(org.springframework.data.mongodb.core.mapping.Document) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 18 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)

Example 19 with Document

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

the class MappingMongoConverterUnitTests method readsPlainDBRefObject.

// DATAMONGO-424
@Test
void readsPlainDBRefObject() {
    DBRef dbRef = new DBRef("foo", 2);
    org.bson.Document document = new org.bson.Document("ref", dbRef);
    DBRefWrapper result = converter.read(DBRefWrapper.class, document);
    assertThat(result.ref).isEqualTo(dbRef);
}
Also used : DBRef(com.mongodb.DBRef) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 20 with Document

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

the class MappingMongoConverterUnitTests method readsCollectionOfDBRefs.

// DATAMONGO-424
@Test
void readsCollectionOfDBRefs() {
    DBRef dbRef = new DBRef("foo", 2);
    BasicDBList refs = new BasicDBList();
    refs.add(dbRef);
    org.bson.Document document = new org.bson.Document("refs", refs);
    DBRefWrapper result = converter.read(DBRefWrapper.class, document);
    assertThat(result.refs).hasSize(1);
    assertThat(result.refs).contains(dbRef);
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBRef(com.mongodb.DBRef) 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