My name is Vasyl Khrystiuk‎ > ‎pub‎ > ‎java‎ > ‎libs‎ > ‎

java MongoDB


Now I am writiing app, that will have persistance in MongoDB.
Why mongo:

Solutions overview

Getting started: 

So, what options is available in java :

    pros: low level control - maximal flexebility
    cons: many code need to write manyally - converting POJO to  DBObject, etc.
        * as a note here - for converting mongo to gson there is a library, in reverse way from json(gson) to mongo: this code

Morphia - excellent ORM for MongoDB
    pros: fast developnent 
    cons: this is ORM, so we have in practice work with strong typed objects - less flexible

Spring data: - MongoDBORM for Spring
    pros: fast development and in-built integration with spring environment
    cons: hard to learn(if there is no expiriance with spring-data before), less flexible, work only with spring
        * tutorials:

hibernate ogm mongodb 

Securing service

Create db admin 

Securing working service:
> show databases
> use mydb5
> db.system.users.find()
// пусто
> use admin
switched to db admin
>db.addUser( { user: "<username>", pwd: "<password>", roles: [ "userAdminAnyDatabase", "userAdmin" ] } )
        "user" : "<username>",
        "pwd" : "<password_hash>",
        "roles" : [
        "_id" : ObjectId("530c14468d296d217b9804c2")

Enable auth

For disable local bypass, add option to mongodb:
mongod --setParameter enableLocalhostAuthBypass=0
or in config file(manual):

actually, need:
remove service
add this to service config:
setParameter = enableLocalhostAuthBypass=0
install service
in case of problem with installing/reinstalling service:

now, after setting options
MongoDB shell version: 2.4.1
connecting to: test
> use admin
switched to db admin
> db.system.users.find()
error: { "$err" : "not authorized for query on admin.system.users", "code" : 16550 }
> db.auth('<username>','<password>');
> db.system.users.find()
{ "_id" : ObjectId("530c14468d296d217b9804c2"), "user" : "<username>", "pwd" : "<password_hash>", "roles" : [ "userAdminAnyDatabase", "userAdmin" ] }

Add user to db

use mydb5
db.addUser( { user: "<username>", pwd: "<password>", roles: [ "readWrite", "dbAdmin", "userAdmin" ] } )

Configure libraries for work with auth

Spring mongo data

<bean id="mongoTemplate" class="">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    <constructor-arg name="mongoConverter" ref="mappingConverter"/>

<mongo:db-factory dbname="mydb5" id="mongoDbFactory" username="username" password="password"/>

Configure remote access


Dumping and restore

Create dump:
timestamp=$(date +_%s) # timestamp
timestamp=$(date +_%y.%m.%d_%H.%M)
mongodump  --username <username> --password <password> -db mydb5 --out mydbdump${timestamp}
mongodump  -db mydb5 --out mydbdump${timestamp}
tar -zcvf dump${timestamp}.tar.gz mydbdump${timestamp}
rm -rf mydbdump${timestamp}

Restore dump:
tar -xzvf dump_14.03.04_16.23.tar.gz
# mongorestore --drop --username <username> --password <password> -db mydb5 dump_14.03.04_16.23\mydb5
#mongorestore --host --port 3017 --drop --username user --password pass /opt/backup/mongodump-2012-10-24