Skip to content

streamx-co/FluentMongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Get in Control of Your Mongo Queries
Patent Pending GitHub Java Version Build Status Maven Central

FluentMongo is a Mongo Java Driver wrapper for writing typesafe queries. See wiki for samples & setup.

Read Operations

Basic Mongo Java driver provides helper methods for writing queries, so the filter condition specification looks like this:

collection.find(and(gte("stars", 2), lt("stars", 5), eq("categories", "Bakery")));

With FluentMongo wrapper:

collection.find(builder.filter(r -> r.getStars() >= 2 && r.getStars() < 5
                                                      && r.getCategories().contains("Bakery")));

No hard coded strings like eq("categories", "Bakery") or strange operators like lt("stars", 5). The filter is a normal Java expression, intellisense and refactoring friendly, with compiler verified type safety. Moreover, we even let write r.getCategories().contains("Bakery") to make the expression as readable and type safe as possible.

The full Sort with Projection example from Mongo manual reads like this:

QueryBuilder<Restaurant> builder = FluentMongo.queryBuilder(Restaurant.class); // can be static

Bson filter     = builder.filter(r -> r.getStars() >= 2 && r.getStars() < 5
                                                        && r.getCategories().contains("Bakery"));
Bson sort       = builder.sort(r -> ascending(r.getName()));
Bson projection = builder.project(r -> fields(include(r.getName(), r.getStars(), r.getCategories()),
                                                                                      excludeId()));

collection.find(filter).sort(sort).projection(projection);

Write Operations

Basic driver:

collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1),
                        set("contact.phone", "228-555-9999"),
                        currentDate("lastModified")));

With FluentMongo wrapper:

// Full intellisense and compiler verified type safety:
Bson filter = builder.filter(r -> eq("57506d62f57802807471dd41"));
Bson update = builder.update(r -> combine(set(r.getStars(), 1),
                                          set(r.getContact().getPhone(), "228-555-9999"),
                                          currentDate(r.getLastModified())));
collection.updateOne(filter, update);

Note: FluentMongo does not replace the official driver. For each operation it parses the expression and calls the suitable helper method in the driver.

License

This work is dual-licensed under Affero GPL 3.0 and Lesser GPL 3.0. The source code is licensed under AGPL and official binaries under LGPL.

Therefore the library can be used in commercial projects.

SPDX-License-Identifier: AGPL-3.0-only AND LGPL-3.0-only