Are You Productive With MongoDB From Java?
Are You Productive With MongoDB From Java?
Explore a solution to issues with the official MongoDB Java driver API.
Join the DZone community and get the full member experience.
Join For FreeMongoDB is doing great, firmly holding a prestigious #5 on the db-engines ranking site. It means that we, developers, are using it a lot.
You might also be interested in: Learn MongoDB With Java 8 (Part 1)
When looking at the official driver API, we can see an effort to make an API as fluent as possible:
collection.find(and(gte(“stars”, 2), lt(“stars”, 5), eq(“categories”, “Bakery”)));
Yet, there are many issues:
- All fields are Strings without auto-completion.
- No type safety on operators (Is
stars
a number? Iscategories
a collection (?) of Strings or...?!). - Do we really like using
gte
for>=
? I mean, is it easy to read and understand this expression? - And finally, when the schema changes, we are back to 80' with find-replace...
Fortunately, there is a simple and effective solution to those issues — FluentMongo, which adds the missing ingredient to the API — type safety and Java integration. It lets you use normal Java to write filters, projections, updates, sorts, and indexes. For example:
collection.find(builder.filter(r -> r.getStars() >= 2 && r.getStars() < 5 &&
r.getCategories().contains("Bakery")));
You can be both productive with MongoDB and write a maintainable code now!
Further Reading
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}