Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

CAST supports MongoDB via its NoSQL for Java extension. Details about how this support is provided for Java with Spring Data source code is discussed below.

Supported Libraries

...

Image Removed

...

Image Removed

...

Image Removed

Supported Operations

...

  • bulkOps
  • insert
  • insertAll
  • save
  • saveAll

...

  • aggregate
  • aggregateAndReturn
  • aggregateStream
  • count
  • estimatedCount
  • exists
  • existsById
  • findOn
  • findDistinct
  • findById
  • findOne
  • findAll
  • findAllById
  • geoNear
  • group

...

  • bulkOps
  • dropCollection
  • remove
  • findAndRemove
  • findAllAndRemove
  • dropCollection
  • delete
  • deleteById
  • deleteAll

...

  • bulkOps
  • updateFirst
  • updateMulti
  • upsert
  • update
  • findAndModify

Objects

...

Image Removed

...

Image Removed

...

Image Removed

...

Image Removed

...

Image Removed

...

Links

...

Between Mongo Objects (Collection → Database → Connection → Project)

...

  • executeQuery
  • mapReduce
  • stream

...

useSelectLink

...

  • aggregate
  • aggregateAndReturn
  • aggregateStream
  • count
  • estimatedCount
  • exists
  • existsById
  • findOn
  • findDistinct
  • findById
  • findOne
  • findAll
  • findAllById
  • geoNear
  • group

...

  • bulkOps
  • updateFirst
  • updateMulti
  • upsert
  • update
  • findAndModify

...

  • bulkOps
  • dropCollection
  • remove
  • findAndRemove
  • findAllAndRemove
  • dropCollection
  • delete
  • deleteById
  • deleteAll

...

  • bulkOps
  • insert
  • insertAll
  • save
  • saveAll

What results can you expect

Once the analysis/snapshot generation has completed, you can view the results in the normal manner (for example via CAST Enlighten). Some examples are shown below.

Creation of connection and database object using MongoTemplate (Spring Data)

...

languagejava

...

Supported till version 3.2.x

Supported Operations

For Storing the data

  • Expand
    titleInsert Operation 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.ReactiveMongoRepository.insert
    • 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.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


For Fetching the data


Expand
titleSelect Operation 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.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


For Removing the data


Expand
titleDelete Operation 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.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


For Updating the data


Expand
titleUpdate 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

...

  • .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

...

Image Removed

Creation of connection and database object using MongoRepository (Spring Data)

...

  • .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

IconDescription

Image Added

Java MongoDB database

Image Added

Java MongoDB collection

Image Added

Java unknown MongoDB database

Image Added

Java unknown MongoDB collection

Links

Link typeSource and destination of linkMethods supported
parentLink

Between Mongo Objects (Collection → Database → Project)


useLinkBetween the caller Spring Data Java Method objects and Mongo Collection Object
  • mapReduce

useSelectLink


Expand
  • find
  • findAll
  • findById
  • findAllById
  • findAll
  • count
  • existsById
  • exists
  • findAndRemove
  • findAllAndRemove
  • findAndReplace
  • findAndModify
  • findOne
  • stream
  • group
  • geoNear
  • aggregate
  • aggregateStream
  • aggregateAndReturn
  • executeQuery
  • getCollection
  • findDistinct


useUpdateLink
  • upsert
  • update
  • updateFirst
  • updateOne
  • updateMulti
  • findAndModify
  • findAndReplace
useDeleteLink
  • deleteAll
  • deleteById
  • delete
  • findAndRemove
  • dropCollection
  • remove
  • findAllAndRemove
useInsertLink
  • save
  • saveAll
  • insert
  • insertAll
  • upsert

What results can you expect

Once the analysis/snapshot generation has completed, you can view the results in the normal manner (for example via CAST Enlighten). Some examples are shown below.

Code Block
languagejava

Creation of database object 

With application.properties

Expand


Code Block
languagejava
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=hc4

logging.level.org.springframework.data=debug
logging.level.=error

...

Image Added

With Java configuration file

Expand


Code Block
languagejava
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");
	}
}

Image Removed

You can also use the CAST Management Studio option View Analysis Unit Content to see the objects that have been created following the analysis:

Image Removed

Marking a connection, Database and Collection as Unknown

When there is no connection object identified but there is CRUD Operation from Mongo API then the connection is marked as Unknown Object. Same is the case with Database and Collection Object.

Mongo Spring Data Select Operation

Code Block
languagejava
titleMongo Spring Data Select Operation
User savedUser = mongo.findOne(searchUserQuery, User.class);

Image Removed

Mongo Spring Data Update Operation

Code Block
languagejava
titleMongo Spring Data Update Operation
mongo.updateFirst(searchUserQuery, Update.update("password", "new password"),
				Candidate.class);
mongo.updateMulti(searchUserQuery, Update.update("password", "new password"),
				Candidate.class);

Image Removed

Mongo Spring Data Insert Operation

Code Block
languagejava
titleMongo Spring Data Insert Operation
mongo.insert(employee);

Image Removed

Mongo Spring Data Delete Operation

Code Block
languagejava
titleMongo Spring Data Delete Operation
mongo.dropCollection(Employee.class);

Image Removed

Mongo Spring Data Generated Query Methods

Info
Support available in ≥ NoSQL for Java - 1.5.

With defined method with parameter 

Code Block
languagejava
public interface UserRepository extends MongoRepository<User, String>, QuerydslPredicateExecutor<User> {
   
    List<User> findByNameEndingWith(String regexp);
}

Image Added

Insert Operation

Expand


Code Block
@Component("storeOrder")
public class StoreOCEOrder implements Processor {

	private static final Logger log = LoggerFactory.getLogger(StoreOCEOrder.class);

	@Autowired
	private OceOrderRepository repository;

	public void process(Exchange exg) throws Exception {
		repository.save(oceResponse);
	}
}

Image Added


Expand


Code Block
languagejava
public 
void givenUsersExist_whenFindingUserWithNameEndWithC_thenUsersAreFound(
String saveAlerts(List<FanAlerts> documentsToSave) {
User user = new User(); user.setName("Eric"); user.setAge(45); mongoOps.insert(user); user = new User(); user.setName("Antony"); user.setAge(33); mongoOps.insert(user); user = new User(); user.setName("Alice"); user.setAge(35); mongoOps.insert(user); List<User> users = userRepository.findByNameEndingWith("c"); assertThat(users.size(), is(1)); }

Image Removed

...

		LOGGER.debug("FanAlertsRepository.saveAlerts()");
		mongoOperation.insert(documentsToSave, FanAlerts.class);
		return AlertServiceConstants.ALERT_CREATED_MESSAGE;
	}

Image Added

Update Operation

Expand
titleUpdate Operation


Code Block
languagejava
public Response deleteCSPProfileLocation(CSPProfileDeleteLocationRequest req) {
	UpdateResult result = collection.getExposedDocCollection().updateOne(filter, cond);
}

Image Added


Delete Operation

Expand
titleDelete Operation


Code Block
languagejava
public 
interface
Response 
UserRepository extends MongoRepository<User
deleteTn(String siteId, 
String>,
String 
QuerydslPredicateExecutor<User>
tn) {
		DeleteResult deleteres = mongoCon.getCollection()
@Query("{ 'age' : { $gt: ?0, $lt: ?1 } }") List<User> findUsersByAgeBetween(int ageGT, int ageLT); }
					.deleteOne(new Document().append(DETAILSSITE_IDEN, siteId).append(DETAILS_TN, tn));
}

Image Added


Select Operation

...

Expand


Code Block
languagejava
public void 
givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound
elkFailureReprocess() {
	Document 
User user = new User(); user.setAge(20); user.setName("Jon"); mongoOps.insert(user); user = new User(); user.setAge(50); user.setName("Jon"); mongoOps.insert(user); user = new User(); user.setAge(33); user.setName("Jim"); mongoOps.insert(user); List<User> users = userRepository.findUsersByAgeBetween(26, 40); assertThat(users.size(), is(1)); }

Image Removed

Limitations

getTnDoc = (Document) mongoCon.getCollection("TN").find(eq("details.tn", id)).first();
}

Image Added

Query Methods 

Expand
titleQuery Method


Code Block
languagejava
public interface OrderCountersRepository extends MongoRepository<Counters, String> {
	@Query(value = "{'_id':?0}")
	public Counters findById(String orderId);
}

Image Added

Evolution

  • Query methods with and without @Query annotation are supported. 
  • Support for Reactive Mongo Repository
  • Support for Reactive Mongo Template and Reactive Mongo Operations
  • Support for APIs belonging to Mongo Collection of com.mongodb.client.MongoCollection
  • No connection objects are created. Only databases and collection objects are created.

Limitations

  • Resolution of Database and Collection is limited, "Unknown" is used when not resolved.
  • useLink will be create for the methods 'executeQuery', 'mapReduce' and 'stream' as the type of link is not determined.
  • 'useLink' will be created in the case of user defined query generated methods. Not all Query Methods will be created.

Future

  • Support for Reactive Mongo RepositoryQuery method results in link between query method and repository collection (domain entity) and not between the actual caller method and repository collection