-
Notifications
You must be signed in to change notification settings - Fork 0
MongoDB
Mahdi Sheikh Hosseini edited this page Sep 11, 2025
·
1 revision
MicroFox MongoDB provides a simple, lightweight way to integrate MongoDB into MicroFox applications.
It uses the official MongoDB Java Driver under the hood but hides boilerplate code with easy configuration and POJO-based collections.
You can:
- Register MongoDB connections with a single method
- Access typed MongoCollection objects directly
- Perform CRUD operations using native MongoDB APIs
Add the following Maven dependency to your project:
<dependency>
<groupId>ir.moke.microfox</groupId>
<artifactId>microfox-mongo</artifactId>
<version>${microfox.version}</version>
</dependency>We register a MongoDB database using MongoConnectionInfo and MongoFactory:
MongoConnectionInfo info = new MongoConnectionInfo(
"admin", // username
"adminpass", // password
"127.0.0.1", // host
27017, // port
"test", // database name
"authSource=admin"// options
);
MongoFactory.registerMongoDatabase("mongo-db", info); Parameters:
- Username & Password → MongoDB authentication credentials
- Host & Port → Server connection details
- Database Name → Database to be used
- Options → Extra connection settings (e.g., authSource=admin)
Entities represent MongoDB collections using the @Collection annotation:
@Collection("persons")
public class Person {
private String name;
private String family;
private Long id;
public Person() {}
// Getters & Setters
}- @Collection("name") → Maps the class to a MongoDB collection name.
- Fields are mapped automatically using POJO mapping.
1. Insert Document
MongoCollection<Person> collection = MicroFox.mongo("mongo-db", Person.class);
Person p = new Person("Mahdi", "Sheikh Hosseini", 12L);
collection.insertOne(p);2. Find All Documents
List<Person> people = collection.find().into(new ArrayList<>());3. Find with Filter
Person result = collection.find(eq("name", "Mahdi")).first();4. Update Document
collection.updateOne(eq("id", 12L), set("family", "Hosseini Updated"));5. Delete Document
collection.deleteOne(eq("id", 12L));- Lightweight: Minimal setup, no heavy frameworks
- POJO Mapping: Work directly with Java objects
- Flexible: Full access to native MongoDB driver APIs
- Multiple Databases: Register multiple identities easily
- Consistent Style: Same approach as MicroFox JPA