Support of MongoDB for Spring Data
CAST supports MongoDB via its com.castsoftware.nosqljava extension. Details about the support provided for Java with Spring Data source code is discussed below.
Supported Libraries
Library | Version | Supported |
---|---|---|
Spring Data MongoDB | Up to: 3.2.x |
Supported Operations
Operation | Scenario | Methods Supported |
---|---|---|
Insert | Repository APIs | org.springframework.data.repository.CrudRepository.save org.springframework.data.repository.CrudRepository.saveAll org.springframework.data.repository.ReactiveCrudRepository.save org.springframework.data.repository.ReactiveCrudRepository.saveAll org.springframework.data.repository.MongoRepository.insert org.springframework.data.repository.MongoRepository.save org.springframework.data.repository.MongoRepository.saveAll org.springframework.data.repository.ReactiveMongoRepository.saveAll org.springframework.data.repository.MongoRepository.save org.springframework.data.repository.ReactiveMongoRepository.insert |
Insert | Template and operation APIs | org.springframework.data.mongodb.core.MongoTemplate.insert org.springframework.data.mongodb.core.MongoTemplate.insertAll org.springframework.data.mongodb.core.MongoTemplate.save org.springframework.data.mongodb.core.MongoTemplate.upsert org.springframework.data.mongodb.core.BulkOperations.insert org.springframework.data.mongodb.core.BulkOperations.upsert org.springframework.data.mongodb.core.ReactiveMongoTemplate.insert org.springframework.data.mongodb.core.ReactiveMongoTemplate.insertAll org.springframework.data.mongodb.core.ReactiveMongoTemplate.save org.springframework.data.mongodb.core.ReactiveMongoTemplate.upsert org.springframework.data.mongodb.core.MongoOperations.insert org.springframework.data.mongodb.core.MongoOperations.insertAll org.springframework.data.mongodb.core.MongoOperations.save org.springframework.data.mongodb.core.ReactiveMongoOperations.insert org.springframework.data.mongodb.core.ReactiveMongoOperations.insertAll org.springframework.data.mongodb.core.ReactiveMongoOperations.upsert |
Select | Repository APIs | org.springframework.data.repository.CrudRepository.findAll org.springframework.data.repository.CrudRepository.count org.springframework.data.repository.CrudRepository.findById org.springframework.data.repository.CrudRepository.findAllById org.springframework.data.repository.CrudRepository.existsById org.springframework.data.repository.PagingAndSortingRepository.findAll org.springframework.data.repository.QueryByExampleExecutor.count org.springframework.data.repository.QueryByExampleExecutor.exists org.springframework.data.repository.QueryByExampleExecutor.findAll org.springframework.data.repository.QueryByExampleExecutor.findOne org.springframework.data.repository.ReactiveCrudRepository.findAll org.springframework.data.repository.ReactiveCrudRepository.count org.springframework.data.repository.ReactiveCrudRepository.findById org.springframework.data.repository.ReactiveCrudRepository.findAllById org.springframework.data.repository.ReactiveCrudRepository.existsById org.springframework.data.repository.ReactiveQueryByExampleExecutor.count org.springframework.data.repository.ReactiveQueryByExampleExecutor.exists org.springframework.data.repository.ReactiveQueryByExampleExecutor.findAll org.springframework.data.repository.ReactiveQueryByExampleExecutor.findOne org.springframework.data.repository.ReactiveMongoRepository.findAll org.springframework.data.repository.MongoRepository.findAll org.springframework.data.repository.MongoRepository.count org.springframework.data.repository.MongoRepository.findById org.springframework.data.repository.MongoRepository.findAllById org.springframework.data.repository.MongoRepository.existsById org.springframework.data.repository.MongoRepository.findOne org.springframework.data.repository.ReactiveMongoRepository.count org.springframework.data.repository.ReactiveMongoRepository.findById org.springframework.data.repository.ReactiveMongoRepository.findAllById org.springframework.data.repository.ReactiveMongoRepository.existsById org.springframework.data.repository.ReactiveMongoRepository.findOne |
Select | Template and operation APIs | org.springframework.data.mongodb.core.MongoTemplate.getCollection org.springframework.data.mongodb.core.MongoTemplate.find org.springframework.data.mongodb.core.MongoTemplate.findAll org.springframework.data.mongodb.core.MongoTemplate.findAllAndRemove org.springframework.data.mongodb.core.MongoTemplate.findAndModify org.springframework.data.mongodb.core.MongoTemplate.findAndRemove org.springframework.data.mongodb.core.MongoTemplate.findAndReplace org.springframework.data.mongodb.core.MongoTemplate.findById org.springframework.data.mongodb.core.MongoTemplate.findDistinct org.springframework.data.mongodb.core.MongoTemplate.findOne org.springframework.data.mongodb.core.MongoTemplate.exists org.springframework.data.mongodb.core.MongoTemplate.count org.springframework.data.mongodb.core.MongoTemplate.collectionExists org.springframework.data.mongodb.core.MongoTemplate.stream org.springframework.data.mongodb.core.MongoTemplate.group org.springframework.data.mongodb.core.MongoTemplate.geoNear org.springframework.data.mongodb.core.MongoTemplate.aggregate org.springframework.data.mongodb.core.MongoTemplate.aggregateStream org.springframework.data.mongodb.core.MongoTemplate.aggregateAndReturn org.springframework.data.mongodb.core.MongoTemplate.executeQuery org.springframework.data.mongodb.core.ReactiveMongoTemplate.getCollection org.springframework.data.mongodb.core.ReactiveMongoTemplate.find org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAll org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAllAndRemove org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndModify org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndRemove org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndReplace org.springframework.data.mongodb.core.ReactiveMongoTemplate.findById org.springframework.data.mongodb.core.ReactiveMongoTemplate.findDistinct org.springframework.data.mongodb.core.ReactiveMongoTemplate.findOne org.springframework.data.mongodb.core.ReactiveMongoTemplate.exists org.springframework.data.mongodb.core.ReactiveMongoTemplate.count org.springframework.data.mongodb.core.ReactiveMongoTemplate.collectionExists org.springframework.data.mongodb.core.ReactiveMongoTemplate.stream org.springframework.data.mongodb.core.ReactiveMongoTemplate.group org.springframework.data.mongodb.core.ReactiveMongoTemplate.geoNear org.springframework.data.mongodb.core.ReactiveMongoTemplate.aggregate org.springframework.data.mongodb.core.ReactiveMongoTemplate.aggregateStream org.springframework.data.mongodb.core.ReactiveMongoTemplate.aggregateAndReturn org.springframework.data.mongodb.core.ReactiveMongoTemplate.executeQuery org.springframework.data.mongodb.core.MongoOperations.aggregate org.springframework.data.mongodb.core.MongoOperations.aggregateStream org.springframework.data.mongodb.core.MongoOperations.aggregateAndReturn org.springframework.data.mongodb.core.MongoOperations.collectionExists org.springframework.data.mongodb.core.MongoOperations.count org.springframework.data.mongodb.core.MongoOperations.exists org.springframework.data.mongodb.core.MongoOperations.find org.springframework.data.mongodb.core.MongoOperations.findAll org.springframework.data.mongodb.core.MongoOperations.findAllAndRemove org.springframework.data.mongodb.core.MongoOperations.findAndModify org.springframework.data.mongodb.core.MongoOperations.findAndRemove org.springframework.data.mongodb.core.MongoOperations.findAndReplace org.springframework.data.mongodb.core.MongoOperations.findById org.springframework.data.mongodb.core.MongoOperations.findOne org.springframework.data.mongodb.core.MongoOperations.geoNear org.springframework.data.mongodb.core.MongoOperations.getCollection org.springframework.data.mongodb.core.MongoOperations.group org.springframework.data.mongodb.core.ReactiveMongoOperations.aggregate org.springframework.data.mongodb.core.ReactiveMongoOperations.aggregateStream org.springframework.data.mongodb.core.ReactiveMongoOperations.aggregateAndReturn org.springframework.data.mongodb.core.ReactiveMongoOperations.collectionExists org.springframework.data.mongodb.core.ReactiveMongoOperations.count org.springframework.data.mongodb.core.ReactiveMongoOperations.exists org.springframework.data.mongodb.core.ReactiveMongoOperations.find org.springframework.data.mongodb.core.ReactiveMongoOperations.findAll org.springframework.data.mongodb.core.ReactiveMongoOperations.findAllAndRemove org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndModify org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndRemove org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndReplace org.springframework.data.mongodb.core.ReactiveMongoOperations.findById org.springframework.data.mongodb.core.ReactiveMongoOperations.findOne org.springframework.data.mongodb.core.ReactiveMongoOperations.geoNear org.springframework.data.mongodb.core.ReactiveMongoOperations.getCollection org.springframework.data.mongodb.core.ReactiveMongoOperations.group |
Delete | Repository APIs | org.springframework.data.repository.CrudRepository.deleteAll org.springframework.data.repository.CrudRepository.deleteById org.springframework.data.repository.CrudRepository.delete org.springframework.data.repository.ReactiveCrudRepository.deleteAll org.springframework.data.repository.ReactiveCrudRepository.deleteById org.springframework.data.repository.ReactiveCrudRepository.delete org.springframework.data.repository.ReactiveMongoRepository.deleteAll org.springframework.data.repository.ReactiveMongoRepository.deleteById org.springframework.data.repository.ReactiveMongoRepository.delete org.springframework.data.repository.MongoRepository.delete org.springframework.data.repository.MongoRepository.deleteAll org.springframework.data.repository.MongoRepository.deleteById |
Delete | Template and operation APIs | org.springframework.data.mongodb.core.MongoTemplate.findAndRemove org.springframework.data.mongodb.core.MongoTemplate.findAllAndRemove org.springframework.data.mongodb.core.MongoTemplate.dropCollection org.springframework.data.mongodb.core.BulkOperations.remove org.springframework.data.mongodb.core.MongoTemplate.remove org.springframework.data.mongodb.core.ReactiveMongoTemplate.remove org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndRemove org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAllAndRemove org.springframework.data.mongodb.core.ReactiveMongoTemplate.dropCollection org.springframework.data.mongodb.core.ReactiveBulkOperations.remove org.springframework.data.mongodb.core.MongoOperations.dropCollection org.springframework.data.mongodb.core.MongoOperations.findAndRemove org.springframework.data.mongodb.core.MongoOperations.findAllAndRemove org.springframework.data.mongodb.core.MongoOperations.remove org.springframework.data.mongodb.core.ReactiveMongoOperations.dropCollection org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndRemove org.springframework.data.mongodb.core.ReactiveMongoOperations.findAllAndRemove org.springframework.data.mongodb.core.ReactiveMongoOperations.remove |
Update | Template and operation APIs | org.springframework.data.mongodb.core.MongoTemplate.upsert org.springframework.data.mongodb.core.MongoTemplate.update org.springframework.data.mongodb.core.MongoTemplate.updateFirst org.springframework.data.mongodb.core.MongoTemplate.updateMulti org.springframework.data.mongodb.core.MongoTemplate.findAndModify org.springframework.data.mongodb.core.MongoTemplate.findAndReplace org.springframework.data.mongodb.core.BulkOperations.updateOne org.springframework.data.mongodb.core.BulkOperations.updateMulti org.springframework.data.mongodb.core.BulkOperations.upsert org.springframework.data.mongodb.core.BulkOperations.replaceOne org.springframework.data.mongodb.core.ReactiveMongoTemplate.upsert org.springframework.data.mongodb.core.ReactiveMongoTemplate.update org.springframework.data.mongodb.core.ReactiveMongoTemplate.updateFirst org.springframework.data.mongodb.core.ReactiveMongoTemplate.updateMulti org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndModify org.springframework.data.mongodb.core.ReactiveMongoTemplate.findAndReplace org.springframework.data.mongodb.core.ReactiveBulkOperations.updateOne org.springframework.data.mongodb.core.ReactiveBulkOperations.updateMulti org.springframework.data.mongodb.core.ReactiveBulkOperations.upsert org.springframework.data.mongodb.core.MongoOperations.findAndModify org.springframework.data.mongodb.core.MongoOperations.findAndReplace org.springframework.data.mongodb.core.MongoOperations.upsert org.springframework.data.mongodb.core.MongoOperations.updateFirst org.springframework.data.mongodb.core.MongoOperations.updateMulti org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndModify org.springframework.data.mongodb.core.ReactiveMongoOperations.findAndReplace org.springframework.data.mongodb.core.ReactiveMongoOperations.upsert org.springframework.data.mongodb.core.ReactiveMongoOperations.updateFirst org.springframework.data.mongodb.core.ReactiveMongoOperations.updateMulti |
Objects
Icon | Description |
---|---|
Java MongoDB database | |
Java MongoDB collection | |
Java unknown MongoDB database | |
Java unknown MongoDB collection |
Links
Link type | Source and destination of link | Methods supported |
---|---|---|
parentLink | Between Mongo Objects (Collection → Database → Project) |
- |
useLink | Between the caller Spring Data Java Method objects and Mongo Collection Object | mapReduce |
useSelectLink |
Between the caller Spring Data Java Method objects and Mongo Collection Object | find findAll findById findAllById findAll count existsById exists findAndRemove findAllAndRemove findAndReplace findAndModify findOne stream group geoNear aggregate aggregateStream aggregateAndReturn executeQuery getCollection findDistinct |
useUpdateLink | Between the caller Spring Data Java Method objects and Mongo Collection Object | upsert update updateFirst updateOne updateMulti findAndModify findAndReplace |
useDeleteLink | Between the caller Spring Data Java Method objects and Mongo Collection Object | deleteAll deleteById delete findAndRemove dropCollection remove findAllAndRemove |
useInsertLink | Between the caller Spring Data Java Method objects and Mongo Collection Object | save saveAll insert insertAll upsert |
What results can you expect
Some example scenarios are shown below:
Creation of database objects
With application.properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=hc4
logging.level.org.springframework.data=debug
logging.level.=error
With Java configuration file
package com.mkyong;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
@Configuration
@EnableMongoRepositories
public class ApplicationConfig extends AbstractMongoConfiguration{
@Override
@Bean
protected String getDatabaseName() {
return "e-store";
}
@Override
protected String getMappingBasePackage() {
return "com.oreilly.springdata.mongodb";
}
@Override
@Bean
public Mongo mongo() throws Exception {
// TODO Auto-generated method stub
return new Mongo("localhost");
}
}
Insert Operation
public Product getMrepefOne () {
Product product = new Product("LN1", "London", 5.0f);
productRepository.save(product);
return productRepository.findOne("LN1");
}
@Override
public BulkWriteResult bulk(Collection<? extends T> objects) {
Assert.notNull(objects, "Objects must not be null!");
return mongoTemplate.bulkOps(bulkMode != null ? bulkMode : BulkMode.ORDERED, Product.class)
.insert(new ArrayList<>(objects)).execute();
}
Update Operation
public String updateAlertStatus(String alertId) {
LOGGER.debug("FanAlertsRepository.deleteAlert() with param{0}" + alertId);
Query query = new Query();
query.addCriteria(Criteria.where(AlertServiceConstants._ID_FILED).is(new ObjectId(alertId)));
FanAlerts fanAlert = mongoOperation.findOne(query, FanAlerts.class,
AlertServiceConstants.FAN_ALERTS_COLLECTION);
if (fanAlert == null) {
return AlertServiceConstants.failure;
Delete Operation
public Response deleteTn(String siteId, String tn) {
DeleteResult deleteres = mongoCon.getCollection()
.deleteOne(new Document().append(DETAILSSITE_IDEN, siteId).append(DETAILS_TN, tn));
}
Select Operation
public GeoResults getProductByLocation (double longitude,double latitude,double distance){
Point p = new Point(longitude, latitude);
NearQuery nearQuery = NearQuery.near(p, Metrics.KILOMETERS).maxDistance(distance);
return mongoTemplate.geoNear(nearQuery, Product.class);
}
Query Methods
@Query("{ 'age' : { $gt: ?0, $lt: ?1 } }")
List<User> findUsersByAgeBetween(int ageGT, int ageLT);
Known Limitations
- Resolution of Database and Collection is limited, “Unknown” is used when not resolved.
- Query method results in link between query method and repository collection (domain entity) and not between the actual caller method and repository collection