Skip to main content

Commonly used methods of MongoOperations in Spring Data MongoDB

 Some commonly used methods of `MongoOperations` in Spring Data MongoDB, along with examples:

1. Insert Documents:
   - insert(Object objectToSave)`: Inserts a document into the collection.

    
YourEntity entity = new YourEntity("value1", "value2");
     mongoOperations.insert(entity);



2. Find Documents:
   - find(Query query, Class<T> entityClass)`: Finds documents matching the given query.

    
Query query = new Query(Criteria.where("field1").is("value1"));
     List<YourEntity> result = mongoOperations.find(query, YourEntity.class);



   - findOne(Query query, Class<T> entityClass)`: Finds the first document matching the query.

    
Query query = new Query(Criteria.where("field1").is("value1"));
     YourEntity entity = mongoOperations.findOne(query, YourEntity.class);

    

3. Update Documents:
   - updateFirst(Query query, Update update, Class<T> entityClass)`: Updates the first document matching the query.

    
Query query = new Query(Criteria.where("field1").is("value1"));
     Update update = new Update().set("field2", "new value");
     mongoOperations.updateFirst(query, update, YourEntity.class);



   - `updateMulti(Query query, Update update, Class<T> entityClass)`: Updates multiple documents matching the query.

    
Query query = new Query(Criteria.where("field1").is("value1"));
     Update update = new Update().set("field2", "new value");
     mongoOperations.updateMulti(query, update, YourEntity.class);



4. Delete Documents:
   - remove(Query query, Class<T> entityClass)`: Removes documents matching the query.

    
Query query = new Query(Criteria.where("field1").is("value1"));
     mongoOperations.remove(query, YourEntity.class);

    

   - remove(Object object)`: Removes the given document.

    
YourEntity entity = new YourEntity("value1", "value2");
     mongoOperations.remove(entity);

    

5. Aggregation:
   - aggregate(TypedAggregation<T> aggregation, Class<O> outputType)`: Performs an aggregation operation.
    
    
TypedAggregation<YourEntity> agg = newAggregation(
         match(Criteria.where("field1").is("value1")),
         group("field2").count().as("count")
     );
     AggregationResults<AggregationResult> results = mongoOperations.aggregate(agg, AggregationResult.class);
     List<AggregationResult> resultList = results.getMappedResults();

    

6. Indexing:
   - indexOps(Class<T> entityClass)`: Provides access to index operations for the specified entity class.
    
    
IndexOperations indexOps = mongoOperations.indexOps(YourEntity.class);
     indexOps.ensureIndex(new Index().on("field1", Sort.Direction.ASC));

    

These are just a few examples of the methods available in `MongoOperations`. You can explore the Spring Data MongoDB documentation for more details and additional methods to suit your specific use cases.

Comments

Popular posts from this blog

Scrum - Transparency, Inspection , and Adaptation

What is Scrum ? Scrum is a light weight agile framework that helps people, organization and team to develop the software in iterative and incremental process. Three Pillars of Scrum Transparency - Giving visibility to the significant aspects of the process to those responsible for the outcome. Inspection - Timely checks on the progress toward a sprint goal to detect undesirable variances Adaptation - Adjusting process as soon as possible to minimize any further deviation or issues   These three pillars of the scrum is achieved through different ceremonies and artifacts of the scrum. We can map the pillars as follows Scrum Values  Scrum is built with following 5 values  Commitment - People personally commit to achieving the goals of the Scrum Team Focus - Everyone focuses on the work of the Sprint and the goals of the Scrum Team Openness - The scrum team and its stakeholders agree to be open about all the work and challenges to perform the work Respect - Scrum Team me...

CI/CD - Continuous integration and continuous delivery

  Old school fashion Integration Let us look into the detail how things are happening before CI/CD process, the old school way. If we take an example of a E-Learning application, there will have product management who creates the vision and defines the requirements for the application according to the market needs. There will have an IT depart who develops, deploys and maintains the application according to the requirements from product team. If we do a closer look into the IT department, there will have programers or different team of programmers who develops the feature according to the instructions from product team. Once it is developed, the source code is passed to build and integration team who then builds these different source code and compile it. Once it is integrated and compiled, it is passed to operations team who take care the deployments. They deploy first in test environment and then passed to Quality Assurance team. Quality assurance team who work closel...

SOLID principles

SOLID is an acronym that represents five design principles in object-oriented programming and software development. These principles were introduced by Robert C. Martin (also known as Uncle Bob) and are aimed at creating robust, maintainable, and scalable software systems. Each principle focuses on a specific aspect of software design and encourages developers to write code that is easy to understand, extend, and modify. Let's go through each of the SOLID principles: Single Responsibility Principle (SRP): The SRP states that a class should have only one reason to change, meaning it should have a single responsibility. In other words, a class should have one primary purpose or functionality, and it should not be responsible for more than that. This helps in keeping classes small, focused, and easier to maintain. When a class has multiple responsibilities, changes in one area can inadvertently affect other areas, leading to potential bugs and code entanglement.
 Open/Closed Principle...