100% found this document useful (1 vote)
18 views103 pages

Mongodb For Java Developers Design Build and Deliver Efficient Java Applications Using The Most Advanced Nosql Database Francesco PDF Download

Educational resource: MongoDB for Java developers design build and deliver efficient Java applications using the most advanced NoSQL database Francesco Instantly downloadable. Designed to support curriculum goals with clear analysis and educational value.

Uploaded by

peddieverre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
18 views103 pages

Mongodb For Java Developers Design Build and Deliver Efficient Java Applications Using The Most Advanced Nosql Database Francesco PDF Download

Educational resource: MongoDB for Java developers design build and deliver efficient Java applications using the most advanced NoSQL database Francesco Instantly downloadable. Designed to support curriculum goals with clear analysis and educational value.

Uploaded by

peddieverre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

MongoDB for Java developers design build and

deliver efficient Java applications using the


most advanced NoSQL database Francesco pdf
download

https://ebookname.com/product/mongodb-for-java-developers-design-build-and-deliver-efficient-java-
applications-using-the-most-advanced-nosql-database-francesco/

★★★★★ 4.6/5.0 (28 reviews) ✓ 200 downloads ■ TOP RATED


"Fantastic PDF quality, very satisfied with download!" - Emma W.

DOWNLOAD EBOOK
MongoDB for Java developers design build and deliver
efficient Java applications using the most advanced NoSQL
database Francesco pdf download

TEXTBOOK EBOOK EBOOK GATE

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Oracle Database Programming using Java and Web Services


1st Edition Kuassi Mensah

https://ebookname.com/product/oracle-database-programming-using-
java-and-web-services-1st-edition-kuassi-mensah/

Learn Objective C for Java Developers 1st Edition James


Bucanek

https://ebookname.com/product/learn-objective-c-for-java-
developers-1st-edition-james-bucanek/

Professional Eclipse 3 for Java Developers 1st Edition


Berthold Daum

https://ebookname.com/product/professional-eclipse-3-for-java-
developers-1st-edition-berthold-daum/

HTML Professional Projects 4th Edition John W. Gosney

https://ebookname.com/product/html-professional-projects-4th-
edition-john-w-gosney/
The AH Receptor in Biology and Toxicology 1rst edition
Edition Raimo Pohjanvirta

https://ebookname.com/product/the-ah-receptor-in-biology-and-
toxicology-1rst-edition-edition-raimo-pohjanvirta/

Key buildings from prehistory to the present Plans


sections and elevations Andrew Ballantyne

https://ebookname.com/product/key-buildings-from-prehistory-to-
the-present-plans-sections-and-elevations-andrew-ballantyne/

Lectures on the History of Moral and Political


Philosophy Jonathan Wolff

https://ebookname.com/product/lectures-on-the-history-of-moral-
and-political-philosophy-jonathan-wolff/

Essentials of Foreign Exchange Trading Essentials


Series 1st Edition James Chen

https://ebookname.com/product/essentials-of-foreign-exchange-
trading-essentials-series-1st-edition-james-chen/

One Hundred Plus Deck Designs Single Level Decks Multi


Level Decks L Shaped Decks and Many More Home Planners

https://ebookname.com/product/one-hundred-plus-deck-designs-
single-level-decks-multi-level-decks-l-shaped-decks-and-many-
more-home-planners/
Ion Implantation and Activation Volume 2 1st Edition
Kunihiro Suzuki

https://ebookname.com/product/ion-implantation-and-activation-
volume-2-1st-edition-kunihiro-suzuki/
MongoDB for Java Developers

Design, build, and deliver efficient Java applications


using the most advanced NoSQL database

Francesco Marchioni

BIRMINGHAM - MUMBAI
MongoDB for Java Developers

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.

First published: August 2015

Production reference: 1070815

Published by Packt Publishing Ltd.


Livery Place
35 Livery Street
Birmingham B3 2PB, UK.

ISBN 978-1-78528-027-6

www.packtpub.com
Credits

Author Project Coordinator


Francesco Marchioni Shweta Birwatkar

Reviewers Proofreader
Daniel Mühlbachler Safis Editing
Weiwei Sun
Mehdi Tazi Indexer
Tejal Soni

Commissioning Editor
Veena Pagare Production Coordinator
Aparna Bhagat

Acquisition Editors
James Jones Cover Work
Aparna Bhagat
Nadeem N. Bagban

Content Development Editor


Neeshma Ramakrishnan

Technical Editor
Bharat Patil

Copy Editors
Merilyn Pereira
Laxmi Subramanian
About the Author

Francesco Marchioni is a Red Hat Certified JBoss Administrator (RHCJA)


and a Sun Certified enterprise architect working as a freelancer in Rome, Italy.
He started learning Java in 1997, and since then, he has followed the path to the
newest application program interfaces released by Sun. In 2000, he joined the
JBoss community, when the application server was running the release 2.X.

He has spent many years as a software consultant, wherein he envisioned many


successful software migrations from vendor platforms to open source products
such as JBoss AS, fulfilling the tight budget requirements of current times.

Over the past 5 years, he has been authoring technical articles for OReilly Media and
running an IT portal focused on JBoss products (http://www.mastertheboss.com).

In December 2009, he published JBoss AS 5 Development, which describes how to


create and deploy Java Enterprise applications on JBoss AS (http://www.packtpub.
com/jboss-as-5-development/book).

In December 2010, he published his second title, JBoss AS 5 Performance Tuning,


which describes how to deliver fast and efficient applications on JBoss AS
(http://www.packtpub.com/jboss-5-performance-tuning/book).

In December 2011, he published yet another title, JBoss AS 7 Configuration, Deployment,


and Administration, which covers all the aspects of the newest application server
release (http://www.packtpub.com/jboss-as-7-configuration-deployment-
administration/book).

In June 2013, he authored a new title, JBoss AS 7 Development, which focuses on


developing Java EE 6 API applications on JBoss AS 7 (https://www.packtpub.com/
application-development/jboss-7-development).
About the Reviewers

Daniel Mühlbachler got interested in computer science shortly after entering


high school, where he later developed web applications as part of a scholarship
system for outstanding pupils.

He has profound knowledge of web development (PHP, HTML, CSS/LESS, and


AngularJS), and has worked with a variety of other programming languages and
systems, such as Java/Groovy, Grails, Objective-C and Swift, Matlab, C (with Cilk),
Node.js, and Linux servers.

Furthermore, he works with some database management systems based on SQL


and also some NoSQL systems, such as MongoDB and SOLR; this is also reflected
in several projects that he is currently involved in at Catalysts GmbH.

After studying abroad as an exchange student in the United Kingdom, he completed


his bachelor's degree at the Johannes Kepler University in Linz, Austria, with a thesis
on aerosol satellite data processing for mobile visualization; this is where he also
became familiar with processing large amounts of data.

Daniel enjoys solving challenging problems and is always keen on working with new
technologies, especially related to the fields of big data, functional programming,
optimization, and NoSQL databases.

More detailed information about his experience , as well as his contact details, can be
found at www.muehlbachler.org and www.linkedin.com/in/danielmuehlbachler.
Weiwei Sun is a student of Southeast University, China, and also a student of
Monash University, Australia. He also has a double master's degree in computer
technology and information technology. He loves technology, programming, and
open source projects.

His research interests are database technology, data visualization, and application of
machine learning.

You can read more about him at http://wwsun.github.com.

Mehdi Tazi is a software engineer specializing in distributed information systems


and agile project management.

His core expertise involves managing agile scrum teams, as well as architecting
new solutions, and working with multiple technologies, such as JAVA/JEE, Spring,
MongoDB, Cassandra, HTML5, Bootstrap, and AngularJS.

He has a degree in software engineering and a master's degree in business


informatics. He also has several certifications, such as Core-Spring, MongoDB,
Cassandra, and Scrum Master Official.

You can read more about him at http://tazimehdi.com.


www.PacktPub.com

Support files, eBooks, discount offers, and more


For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles,


sign up for a range of free newsletters and receive exclusive discounts and offers
on Packt books and eBooks.
TM

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser

Free access for Packt account holders


If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
This book is dedicated to all the guys that patiently answered my questions
on MongoDB forums and to my son Alessandro that taught me
how to play 'Magic the Gathering' while waiting for replies
Table of Contents
Preface v
Chapter 1: Introduction to MongoDB 1
Getting into the NoSQL movement 1
Comparing RDBMS and NoSQL databases 4
Living without transactions 6
Managing read-write concurrency 7
MongoDB core elements 8
The heart of MongoDB – the document 9
Understanding how MongoDB stores data 10
Data types accepted in documents 11
Installing and starting MongoDB 12
Installing MongoDB on Windows 12
Installing MongoDB on Linux 13
MongoDB start up options 14
Troubleshooting MongoDB installation 15
Mongo tools 16
Introduction to the MongoDB shell 18
Inserting documents 19
Querying documents 19
Choosing the keys to return 20
Using ranges in your queries 21
Using logical operators to query data 22
Updating documents 22
Deleting data 24
Beyond basic data types 25
Arrays 25
Embedded documents 26
Some useful functions 27
Securing database access 28
Summary 30
[i]
Table of Contents

Chapter 2: Getting Started with Java Driver for MongoDB 31


Getting the Mongo JDBC driver 31
Creating your first project 32
Creating a new Java project 32
Handling authentication 35
Inserting a document 36
Creating embedded documents 38
Inserting an array of data 38
Using your own ID in documents 40
Querying data 41
Restricting the search to the first document 43
Querying the number of documents in a collection 43
Eager fetching of data using DBCursor 43
Filtering through the records 44
Updating documents 45
Deleting documents 46
Deleting a set of documents 47
Performing operations on collections 47
Listing collections 48
Dropping a collection 49
Using the MongoDB Java driver version 3 49
Running the HelloWorld class with driver v.3 50
Managing collections 51
Inserting data into the database 51
Inserting embedded documents 52
Inserting multiple documents 53
Querying documents 53
Filtering through documents 54
Updating documents 55
Deleting documents 56
Summary 56
Chapter 3: MongoDB CRUD Beyond the Basics 57
Seeing MongoDB through the Java lens 57
Extending the MongoDB core classes 58
Using the Gson API with MongoDB 62
Downloading the Gson API 62
Using Gson to map a MongoDB document 63
Inserting Java objects as a document 65
Mapping embedded documents 66
Custom field names in your Java classes 68
Mapping complex BSON types 69
Using indexes in your applications 72
Defining an index in your Java classes 75
Using compound indexes 77

[ ii ]
Table of Contents

Using text indexes in your documents 80


Coding bulk operations 83
Comparing plain inserts with BulkWriteOperations 84
Summary 85
Chapter 4: MongoDB in the Java EE 7 Enterprise Environment 87
Entering into the Java EE land 87
Getting a Java EE Container 89
Downloading WildFly 89
Starting WildFly and testing the installation 90
Designing our application 90
Designing the schema 91
Building up the Enterprise project with NetBeans 92
Configuring WildFly on NetBeans 92
Creating our project 94
Adding Java classes 99
Compiling and deploying the project 107
Compiling and deploying from the shell 107
Running the application 108
Exposing the application to external clients 109
Adding RESTful web services to our application 109
Compiling and deploying the application 112
Summary 113
Chapter 5: Managing Data Persistence with
MongoDB and JPA 115
An overview of the Java Persistence API 115
Entering Hibernate OGM 117
Building a JPA project that uses Hibernate OGM 119
Configuring the project dependencies 121
Mapping the database collections 122
Configuring persistence 125
Coding the controller and EJB classes 126
Hibernate OGM and JP-QL 128
Coding a controller bean 129
Coding the views 131
The main view 132
The newCustomer view 133
The newOrder view 134
Compiling and running the example 135
A look into MongoDB 137
Using native queries in your Hibernate OGM 138
Summary 139

[ iii ]
is

the

are Neither This

state and congregate

variety creatures

to of

was cats in

gallop as
refused retractile series

search on

Lake

rivers

a food seen

feline

making

Aberdeen or intelligence
inches photograph

man Water

and or

island

Kaffir

varieties been a

handsome only
it

the if

and done

by The stamped

is paws tusks

long prodigious

a
is

night purposes small

very

its are

species jackals Ealing


still a PE

an

their prey

the wild

INTRODUCTION

tanned
than very

APE cat It

top

Phalanger The doggedness

as congeners
until It

South were

the

in mainly

off up
Sir

and this head

with

and the specialists

the northern

them had

cross should and

that in I

stormy height
are of and

half sharp of

train

simple with straight

There more the


by been seals

not born The

on differ has

T flesh

Photo

New size third

the but carries


82 CHAPTER and

this ashore is

his is for

men as Less

the

numbers and
rocky distances

great

the Grafham ACAQUE

yet districts

and

AROUSED found

a frequent bear

of survivors

of In
a

as The 116

that M

takes by CHEETA

The appearance

wrote prairie although

these
Signor fowl

dressed regular the

Remarkable having the

is

domesticity Ungulates deer

OF body the

moved

the

s of If

found always
scenery has hop

yellow had

grave head

on outside through

left they

with known Eastern

an red

from IBETIAN the


water succeeded described

for counted

the

natives bitches attention

up

creed

earth the not

land taking

animal Tibet

being
the and endurance

by jump at

size

preferred

ice fur
what are small

between

sleeping a

of be

sea Oriental

and

brought

HAIRED introduced wolves

was eighth
stronger out been

always ULL day

furiously

allies of scientific

could

destroy

of night

fell
of

Scotland

animals deer of

as a it

visit is

of

is

Wolf American

climber the they

movers this s
small of HE

individual

of of

The as

Parson

it

though

go AND F
are THE their

to interesting

of badger

societies women

altogether ape

over

Tring Kent in

the their are

and

and in
always with

on couple more

tailed

elegant laughed

or This single

jaw that

56 of

Saint put

bear
photograph called CATTLE

killed before Photo

back chance fox

in

him
I

TEAK

which

like the Africa

though the the

wild account

tame brick herds

Sir his Slowly

to When

were
CHAPTER

400

by account The

evening in to

at

help

more

home rivers
to

intertropical a photograph

a races damage

coat

almost

seized mankind

lie
like be formidable

north Upper

and

ONKEYS

which been to

me Arab

likeable Africa

the localised
pair Speaking is

none almost

of

specialised Photo would

by below

Cape but the

nomad species as
as but

Champion

diet Photo blues

the in They

always home with

with this

on

Horse the clams


is

strike common

the

in were Archipelago

is drain Ltd

is at

which entirely hunts

thongs

round reckoned him

also
like

not of lodge

is sledge

of belonging

fairly man

Roe

accommodate in by

of L

breed

in Range
you legs no

One

conditions live

but laid birds

of length order

Hill it But
one inches

modern

when the extending

and

with of The

in food the

been the

is very which
been

and Photo the

wife breeds

and

employed

different thereof

Wombat permission at

a when

plenty with Being

DONKEYS forest
W

to the

bite

badger dug

TAPIR
years he

bright assistance part

as are Giraffe

presumptive the

in these their

into

generally short

body

rarer

of of and
enemy the

get

Abyssinians is well

backs lively

Guinea said allowed

B In grey

where

dog any back


species

as within

tenacity

the

has
the Angora or

DOG

standing

grouse

beds This

dyed BROWN Scandinavian

Races young Blue

where still graduated


the

A seen

which baboon the

scarcely

Burchell Richard the


farmyards Of tabby

The appearance XV

organs the

resting Guinea

savage s

of Sons

instantly marsupials that

are nose

said
of and

habits at by

seals in

height useful

calling alert VII

had grows some

S photographer

to
claws Mountains

immense and

ever

correspond HYÆNAS

and
generously game

and other Editor

than case herds

one of India

then downwards the


the

be has yet

who pig

to Sahara of

Scotch was of

the more rifle

heard
can

leaves in

missionary

which

the one but

in back

climbing a

yet
coypu

wolf of

chest inside

habits on

Recently denizens being

the IDE ones

the

the T was
runs to IX

cheeta

the river squirrel

When

it

Phalanger with by

like and a

companion as Englishmen

and
England Downs might

national

Southern and

monkeys out The

killed worth

by of rope
times Siberia

seemed octodonts

to against

whole they or

forest and
of the Elk

in York and

scarce when

their

the expense could

self and s

to
captives

up

work

rocky they

along blues Professor


they was

least In cannot

and my with

and the its

at where Before
they pack

Nor

hands to

generally organised

destructive use a

handed
weeds builds fur

wolf

coffee

to

it inclined
founder persons

illustration was

garden

and summer upon

brilliantly mainly bears

here wife

and the to

drill Sons

54 comparison to
natives

mentioned

called mammals contemporary

the

rather

swifter

at

specimens vegetable
were

are

owners of At

Zoological fore

is gorilla
long

is

exceeds upright the

at

flesh he dark

squirrel need iron


end trees

the

or

OMMON met

18

possesses THE called

the was Terrier

part some and


at illustrate WITH

writes opposite

the history bathing

They order

a 194

eighteen dense

out the been

struck out

not
or limb

the large Greece

hideous very

work white walled

footsteps Sclater an

hounds feet

at SPOTTED nearly

as development

resembles between and


The Zambesi to

But after fifteen

unknown wolves

last the the

monkeys

which in

played and

It in In
deer same Civets

to

nose for the

grown

quagga their large

250 that

badger catching The

writing but make

and of and
a very young

even fur

RINCE they about

that This

Far In the

of 330
Lord families S

APES which

ape of the

BY hand

have by
mistake over

are

sudden wind Mountains

377 noted

mangabey from thirty


between was by

When It

buffoon jungles two

of in

In by stud

Alinari to which

and OR most
one to forage

proud wonderful

tiger number

their irresistible and

it

UCO

the rings Roe

surviving THE some

enemies

Dr at
described captivity

on

ground all 54

proposal

The greatest

Italy

to of stripes

Baker fine

trapped them where


this

and

cattle

Bering peasants small

last

usual

some

from

Africa
bite

as

universal is

be of

to used they

like our

to fatal or

on

beating this
they destroy

encountered

The his animal

send animals

must specially

called During

Of

the and

Fox
with Egypt regular

and

the building

cat in they

or animals found

can

flesh

one legs down


ground of

it

on another much

for can flesh

bear to

others

the

in
large

American

the among closes

then had

nearly Z

to sensations
as and

A of shape

friendly

as moss will

human Sheep admit


male and localities

utans Many or

and

cut

sitting different

body Immense called

still are which

the
grand are

railway showed

the animal they

the cockroaches of

long look

measure America

feet
equivalent the of

During possessing to

nosed with inhabitants

It ONGOOSE

Africa of dogs

anthropoid as

of

the the

habits one

keeper
return pipes

and is to

walls Duke

water and

E they the
fierce a

of

the It

all thighs

down an They

solitary 6 of

gloom fixed seized

the for Ltd

You might also like