Mastering MongoDB with Java: Reading Document Ranges
In this tutorial, we dive into how to effectively filter and retrieve documents from a MongoDB collection using Java. This is a crucial skill for any developer working with NoSQL databases.
Step 1: Connecting to MongoDB
First, we create a MongoClient object by providing the host name and port number. This acts as our gateway to the database server.
Step 2: Selecting Your Collection
Once connected, we access our specific database (e.g., "order") and target the "product" collection. This is where our data resides.
Step 3: Filtering Data (Greater Than Logic)
The power of MongoDB lies in filtering. In this example, we use the find() method with a criteria to fetch products where the price is greater than 10,000.
Step 4: Storing Results in ArrayList or Consumer
After filtering, we can handle the data in two modern ways:
ArrayList: Store all matching documents in a list to iterate through later.
Consumer Interface: Use a functional approach to process each document as it is retrieved.
Check Out More Tutorials
If you found this helpful, explore these related videos from my channel:
In Spring Framework, when you inject collections using standard <list> tags, Spring creates an internal default List implementation. However, if your application explicitly requires a specific concrete implementation such as java.util.ArrayList or java.util.LinkedList, Spring provides the ListFactoryBean helper class.
Why Use ListFactoryBean?
Explicit Implementation: Set whether the resulting List bean is backed by ArrayList or LinkedList using the targetListClass property.
Standalone Reusable Bean: Define the list as its own first-class Spring bean and inject it into multiple collaborator beans across your context.
Fine-Grained Control: Easily swap implementations for performance optimization (e.g., fast random access vs. frequent additions/removals) directly in XML.
Step-by-Step Configuration
1. Configuring an ArrayList with ListFactoryBean
To configure a concrete ArrayList, set targetListClass to java.util.ArrayList:
ListFactoryBean provides an explicit, clean way in Spring XML to control concrete List collection types without changing your Java bean source code. Use ArrayList when fast index-based retrieval is needed and LinkedList when elements are frequently added or removed.
publicvoid setShopName(String shopName) { System.out.println("setShopName(String shopName) is "
+ "called by the Spring Container"); this.shopName = shopName; }
publicvoid setListOfAvailableFruits(List<String> listOfAvailableFruits) { System.out.println( "setListOfAvailableFruits(List<String> listOfAvailableFruits) is "
+ "called by the Spring Container"); this.listOfAvailableFruits = listOfAvailableFruits; }
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop"); System.out.println( "Got fruitShop object from the ApplicationContext(Spring Container)");
fruitShop.displayFruitShopInfo();
}
}
Output
Jul 28, 2018 12:08:50 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Sat Jul 28 12:08:50 IST 2018]; root of context hierarchy
Jul 28, 2018 12:08:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
setShopName(String shopName) is called by the Spring Container
setListOfAvailableFruits(List<String> listOfAvailableFruits) is called by the Spring Container
---------------------------------------
Got fruitShop object from the ApplicationContext(Spring Container)
FruitShop [shopName=Fruit Mart, listOfAvailableFruits=[apple, pineapple, orange]]
In the Spring Framework, defining collections directly using the util schema is concise, modern, and readable. Using the util:list element, you can easily instantiate and inject concrete List implementations such as java.util.ArrayList or java.util.LinkedList as first-class, standalone Spring beans.
Why Use util:list?
Explicit Concrete Implementation: Use the list-class attribute to specify whether your bean is an ArrayList or a LinkedList.
Clean and Concise XML: Eliminates verbose factory bean declarations while remaining fully declarative.
Standalone Reusability: Create standalone collection beans that can be referenced across multiple bean definitions via ref.
Step-by-Step Configuration
First, ensure the Spring util namespace is defined at the top of your XML context configuration:
The util:list tag offers a cleaner syntax than traditional factory beans. If your application needs rapid index-based lookups, configure ArrayList; for frequent element insertions or deletions, switch to LinkedList seamlessly via the list-class attribute.
Recommended Spring Collection Tutorials from Ram N Java
publicvoid setShopName(String shopName) { System.out.println("setShopName(String shopName) is "
+ "called by the Spring Container"); this.shopName = shopName; }
publicvoid setListOfAvailableFruits(List<String> listOfAvailableFruits) { System.out.println( "setListOfAvailableFruits(List<String> listOfAvailableFruits) is "
+ "called by the Spring Container"); this.listOfAvailableFruits = listOfAvailableFruits; }
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop"); System.out.println( "Got fruitShop object from the ApplicationContext(Spring Container)");
fruitShop.displayFruitShopInfo();
}
}
Output
Jul 28, 2018 11:41:03 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Sat Jul 28 11:41:03 IST 2018]; root of context hierarchy
Jul 28, 2018 11:41:04 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
setShopName(String shopName) is called by the Spring Container
setListOfAvailableFruits(List<String> listOfAvailableFruits) is called by the Spring Container
---------------------------------------
Got fruitShop object from the ApplicationContext(Spring Container)
FruitShop [shopName=Fruit Mart, listOfAvailableFruits=[apple, pineapple, orange]]