You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Devender Yadav edited this page Aug 16, 2017
·
3 revisions
MongoDB
MongoDB is an open-source document database with the dynamic schema that provides high performance, high availability and automatic scaling. It supports embedded data model that reduces I/O activities and makes reads & writes faster. Secondary indexes in MongoDB make querying faster. Automatic sharding is another key feature of MongoDB distributes collection data across machines.
MongoDB provides features like aggregations, geospatial querying, text search, etc. It also provides transport and storage encryption. MongoDB supports many authentication mechanisms like SCRAM-SHA-1, MongoDB-CR, etc. that clients can use to verify their identity.
Support
Being a JPA provider, Kundera provides support for MongoDB. It allows to perform CRUD and query operation over MongoDB using JPA specifications. Let's look into how to use Kundera over MongoDB as a database.
To use it, you need to have the following dependency in your pom.xml.
Insert, Find, Update and Remove operations are straightforward
PersonMongo person = new PersonMongo();
person.setPersonId("1");
person.setPersonName("John Smith");
person.setAge(32);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mongo_pu");
EntityManager em = emf.createEntityManager();
//Insert data
em.persist(person);
em.clear(); //Clear cache before finding record
//Search for data
Person personFound = em.find(PersonMongo.class, "1");
//Update data
person.setAge(33);
em.merge(person);
//Delete data
em.remove(person);
em.close();
emf.close();
Queries
Find by key
String findById = "Select p from PersonMongo p where p.personId=:personId";
query = em.createQuery(findById);
query.setParameter("personId", “1”);
Range
// Find by greater than and less than clause over non row key
String findAgeByGTELTEClause = "Select p from PersonMongo p where p.age <=:max AND p.age>=:min";
query = em.createQuery(findAgeByGTELTEClause);
query.setParameter("min", 32);
query.setParameter("max", 35);
Between clause
// find by between over non rowkey
String findAgeByBetween = "Select p from PersonMongo p where p.age between :min AND :max";
query = em.createQuery(findAgeByBetween);
query.setParameter("min", 32);
query.setParameter("max", 35);