Giter Site home page Giter Site logo

nephilc / mongodb-springboot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mongodb-developer/mongodb-springboot

0.0 0.0 0.0 75 KB

The project demonstrates basic CRUD examples using MongoDB and SpringBoot

License: Apache License 2.0

Java 100.00%

mongodb-springboot's Introduction

mongodb-springboot

The project demonstrates basic CRUD examples using MongoDB and SpringBoot

For this tutorial, we need to create a Spring Boot project, which can be done easily using Spring Initialzr. It is advantageous to use an IDE (Integrated Development Environment) like Eclipse for this tutorial.

Project Setup

Once the project is setup, we need to create Java class and fields that will be mapped into MongoDB collection and fields.

For the API implementation, we use two approaches:

  1. MongoRepository - We demonstrate all the CRUD operations using this approach.
  2. MongoTemplate - It is used for more control over filters, and also a good choice for aggregations. We will demonstrate update operation using this approach.

CRUD Examples using MongoRepository

To create a document (item) in MongoDB, use the save() method:

groceryItemRepo.save(new GroceryItem("Dried Red Chilli", "Dried Whole Red Chilli", 2, "spices"));

To Read all the documents, use findAll() method:

groceryItemRepo.findAll().forEach(item-> item.getName(),item.getQuantity(),item.getCategory());

To Read documents based on a particular field, like name or category, by specifying the query parameters:

@Query("{name:'?0'}")
GroceryItem findItemByName(String name);

@Query(value="{category:'?0'}", fields="{'name' : 1, 'quantity' : 1}")
List<GroceryItem> findAll(String category);

To update a document using MongoRepository, we should retrieve the specific documents and save the new values. For example, to update a category, do the following:

List<GroceryItem> list = groceryItemRepo.findAll(category);
	 
list.forEach(item -> {
   // Update the category in each document
	 item.setCategory(newCategory);
});
	 
// Save all the items in database
List<GroceryItem> itemsUpdated = groceryItemRepo.saveAll(list);

To delete a document, use the delete method. For example, delete an item by ID using the deleteById() method:

groceryItemRepo.deleteById(id);

Update using MongoTemplate

Updates using MongoTemplate are much easier with the out-of-box classes provided by MongoTemplate. Let us update the quantity of a particular item:

Query query = new Query(Criteria.where("name").is(name));
Update update = new Update();
update.set("quantity", newQuantity);		
UpdateResult result = mongoTemplate.updateFirst(query, update, GroceryItem.class);

Disclaimer

This software is not supported by MongoDB, Inc under any of their commercial support subscriptions or otherwise. Any usage is at your own risk.

mongodb-springboot's People

Contributors

ramya1703 avatar sis0k0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.