O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

MongoDB for Java Developers

Book Description

Design, build, and deliver efficient Java applications using the most advanced NoSQL database

About This Book

  • Reuse the skills you have acquired through Hibernate or Spring to promote your applications to use NoSQL storage
  • Explore the list of libraries that are already available to assist you in developing Java EE applications with MongoDB
  • A step-by-step tutorial to create leaner and faster applications using MongoDB

Who This Book Is For

This book is for Java developers and architects that want to learn how to develop Java applications using the most popular NoSQL solution and the use cases of it.

What You Will Learn

  • Install MongoDB and its client tools
  • Set up a basic project that uses a MongoDB driver and perform CRUD operations with it
  • Explore simple strategies for mapping Mongo documents with Java classes
  • Use bulk operations to speed up the creation of massive document
  • Design and develop a Java Enterprise application that uses MongoDB as storage
  • Develop and deploy an application that uses Hibernate OGM as a persistence layer for your entities
  • Use Spring Data and Spring Boot to leverage micro-services using MongoDB as storage

In Detail

The NoSQL movement is growing in relevance, attracting more and more developers. The MongoDB database is a well-recognized rising star in the NoSQL world. It is a document database, which allows data persistence and enables you to query data in a nested state without any schema constraint and complex joins between documents.

This book provides all the knowledge you need to make MongoDB fit in your application schema, at the best of its capabilities. It starts from a basic introduction to the driver that can be used to perform some low-level interaction with the storage. Then it moves to use different patterns to abstract the persistence layer into your applications, starting from the flexible Google JSON library to the Hibernate OGM Framework and finally landing on the Spring data framework.

By the end of this book, you will know everything you need to use MongoDB in your Java applications.

Style and approach

A simple, tutorial-like approach is used to explain the concepts contained in the book in the simplest possible way. The chapters of this book are arranged so that complexity increases progressively as you gain more experience from the earlier chapters.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. MongoDB for Java Developers
    1. Table of Contents
    2. MongoDB for Java Developers
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introduction to MongoDB
      1. Getting into the NoSQL movement
        1. Comparing RDBMS and NoSQL databases
        2. Living without transactions
        3. Managing read-write concurrency
      2. MongoDB core elements
        1. The heart of MongoDB – the document
          1. Understanding how MongoDB stores data
          2. Data types accepted in documents
      3. Installing and starting MongoDB
        1. Installing MongoDB on Windows
        2. Installing MongoDB on Linux
          1. MongoDB start up options
          2. Troubleshooting MongoDB installation
      4. Mongo tools
      5. Introduction to the MongoDB shell
        1. Inserting documents
        2. Querying documents
          1. Choosing the keys to return
          2. Using ranges in your queries
          3. Using logical operators to query data
        3. Updating documents
        4. Deleting data
        5. Beyond basic data types
          1. Arrays
          2. Embedded documents
        6. Some useful functions
      6. Securing database access
      7. Summary
    9. 2. Getting Started with Java Driver for MongoDB
      1. Getting the Mongo JDBC driver
        1. Creating your first project
          1. Creating a new Java project
          2. Handling authentication
        2. Inserting a document
          1. Creating embedded documents
          2. Inserting an array of data
          3. Using your own ID in documents
        3. Querying data
          1. Restricting the search to the first document
          2. Querying the number of documents in a collection
          3. Eager fetching of data using DBCursor
          4. Filtering through the records
            1. Building more complex searches
        4. Updating documents
        5. Deleting documents
          1. Deleting a set of documents
        6. Performing operations on collections
        7. Listing collections
          1. Dropping a collection
      2. Using the MongoDB Java driver version 3
        1. Running the HelloWorld class with driver v.3
        2. Managing collections
        3. Inserting data into the database
          1. Inserting embedded documents
          2. Inserting multiple documents
        4. Querying documents
          1. Filtering through documents
        5. Updating documents
        6. Deleting documents
      3. Summary
    10. 3. MongoDB CRUD Beyond the Basics
      1. Seeing MongoDB through the Java lens
        1. Extending the MongoDB core classes
        2. Using the Gson API with MongoDB
          1. Downloading the Gson API
          2. Using Gson to map a MongoDB document
          3. Inserting Java objects as a document
          4. Mapping embedded documents
          5. Custom field names in your Java classes
          6. Mapping complex BSON types
      2. Using indexes in your applications
        1. Defining an index in your Java classes
          1. Using compound indexes
          2. Using text indexes in your documents
            1. Searching for text by language
            2. Searching for text by score
      3. Coding bulk operations
        1. Comparing plain inserts with BulkWriteOperations
      4. Summary
    11. 4. MongoDB in the Java EE 7 Enterprise Environment
      1. Entering into the Java EE land
      2. Getting a Java EE Container
        1. Downloading WildFly
        2. Starting WildFly and testing the installation
      3. Designing our application
        1. Designing the schema
      4. Building up the Enterprise project with NetBeans
        1. Configuring WildFly on NetBeans
        2. Creating our project
        3. Adding Java classes
        4. Compiling and deploying the project
          1. Compiling and deploying from the shell
        5. Running the application
        6. Exposing the application to external clients
          1. Adding RESTful web services to our application
          2. Compiling and deploying the application
      5. Summary
    12. 5. Managing Data Persistence with MongoDB and JPA
      1. An overview of the Java Persistence API
      2. Entering Hibernate OGM
      3. Building a JPA project that uses Hibernate OGM
        1. Configuring the project dependencies
        2. Mapping the database collections
        3. Configuring persistence
        4. Coding the controller and EJB classes
          1. Hibernate OGM and JP-QL
        5. Coding a controller bean
        6. Coding the views
          1. The main view
          2. The newCustomer view
          3. The newOrder view
        7. Compiling and running the example
        8. A look into MongoDB
      4. Using native queries in your Hibernate OGM
      5. Summary
    13. 6. Building Applications for MongoDB with Spring Data
      1. Introducing Spring Boot
        1. Getting started with Spring Boot
      2. Getting started with Spring Data
        1. Using the Spring repository to access MongoDB
          1. Coding our Spring Boot application
            1. Mapping Java classes with Spring Data
            2. Running the example
            3. Customizing the repository storage
            4. Using JSON queries in your repository classes
          2. Serving MongoDB using Spring REST
      3. Using the Mongo template component to access MongoDB
        1. Building up the data access layer
        2. Adding the Application class
          1. Creating fine grained queries using Criteria
      4. Summary
    14. Index