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);
}
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")));
}
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);
}
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)));
}
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;
}
Aggregations