Thursday, February 20, 2020

Coming from pure RDBMS and SQL world, persisting object states and querying was always a challenge


Now, to give you a background, Java object state can best be represented as JSON.

- remember JPA Entity class definitions
- in pure RDBMS each of Java object would get represented as individual tables
- querying via SQL gets complex

Enter Mongo DB

Natural question 
https://softwareengineering.stackexchange.com/questions/54373/when-would-someone-use-mongodb-or-similar-over-a-relational-dbms

The immediate and fundamental difference between MongoDB and an RDBMS is the underlying data model. A relational database structures data into tables and rows, while MongoDB structures data into collections of JSON documents. JSON is a self-describing, human readable data format. Originally designed for lightweight exchanges between browser and server, it has become widely accepted for many types of applications.

Pros:
  • MongoDB has a lower latency per query & spends less CPU time per query because it is doing a lot less work (e.g. no joins, transactions). As a result, it can handle a higher load in terms of queries per second and is thus often used if you have a massive # of users.
  • MongoDB is easier to shard (use in a cluster) because it doesn't have to worry about transactions and consistency.
  • MongoDB has a faster write speed because it does not have to worry about transactions or rollbacks (and thus does not have to worry about locking).
  • MongoDB does not have a schema in case you have a special use case that can take advantage of that.
Cons:
  • MongoDB does not support transactions. This is how it obtains most of its benefits.
  • In general, MongoDB creates more work (e.g. more CPU cost) for the client server. For example, to join data one has to issue multiple queries and do the join on the client.
  • Even here in 2017 there is less tooling support for MongoDB than there is for relational databases simply because it is newer. There are also fewer MongoDB experts than their relational counterparts.
Points Often Misunderstood:
  • Both MongoDB and relational databases support indexing. Their query performance is similar in terms of executing large queries.
  • MongoDB does not remove the need for migrations or more specifically, updating your existing data as your schema evolves. For example: If you have an application that relies on a users table to contain certain data, and you modify that table to contain different data (let's say you add a profile picture field), then you will still need to either:
    • Write you application to handle objects for which this property is undefined OR
    • Write a one-time migration to put in a default value for this property OR
    • Write code to provide a default value at query time if this field is not present OR
    • Handle the missing field in some other way

Future Links


[1]
short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments.

http://bsonspec.org/ 

[2]
CRUD operations

https://docs.mongodb.com/guides/server/insert/
https://docs.mongodb.com/guides/server/read_operators/
https://docs.mongodb.com/guides/server/update/
https://docs.mongodb.com/guides/server/delete/

[3]
Query operations in Mongo and analogies with good old RDBMS SQL
https://docs.mongodb.com/manual/tutorial/query-documents/ 

Please note : above link has a good Mongo DB Web Shell to quickly try out commands.
So no need to hunt for VM box and install tarball and put env variable sttings
or pull  a docker image and try out.

You just have to give it try right there
What a way to learn.
LaaS
Learning on Cloud
:)

[4]
Now come in Python
Scripting language to do No SQL Mongo DB operations ( analogical to PL/SQL or .sql files in pure RDBMS world that you did using Oracle Sql Worksheet )

https://api.mongodb.com/python/current/tutorial.html

[5]
New generation needs and reporting and Overview

https://info-mongodb-com.s3.us-east-1.amazonaws.com/MongoDB_Architecture_Guide.pdf

Good read here.
Some experts..
is the best way to create visualizations of MongoDB data anywhere. Build visualizations quickly and easily to analyze complex, nested data. Embed individualcharts into any web application or assemble them into livedashboards for sharing.

Kubernetes Integration

Kubernetes is the industry leading container orchestration platform. It provides you with a consistent automation andmanagement experience anywhere from on-premises infrastructure to the public cloud. Kubernetes users can use theMongoDB Enterprise Operator for Kubernetesthatintegrates with MongoDB Ops Manager to automate andmanage MongoDB clusters. You have full control over yourMongoDB deployment from a single Kubernetes controlplane. You can use the operator with upstream Kubernetes,or with any popular distribution such as Red Hat OpenShiftand Pivotal Container Service (PKS).
[6]

Analogical comparsion with SQL
https://docs.mongodb.com/manual/reference/sql-comparison/


Word of Caution : Do not get carried away by technology and charts.

[7]
Now to integrating Mongo DB queries into Java code
https://www.mongodb.com/blog/post/getting-started-with-mongodb-and-java-part-i

http://central.maven.org/maven2/org/mongodb/mongo-java-driver/


    
        org.mongodb
        mongo-java-driver
        2.12.3
    
 
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")
 
MongoClient mongoClient = new MongoClient();

Where are my tables?

MongoDB doesn’t have tables, rows, columns, joins etc. There are some new concepts to learn when you’re using it, but nothing too challenging.
While you still have the concept of a database, the documents (which we’ll cover in more detail later) are stored in collections, rather than your database being made up of tables of data. But it can be helpful to think of documents like rows and collections like tables in a traditional database. And collections can have indexes like you’d expect.
DB database = mongoClient.getDB("TheDatabaseName");
 
DBCollection collection = database.getCollection("TheCollectionName");
 
person = {
  _id: "jo",
  name: "Jo Bloggs",
  age: 34,
  address: {
    street: "123 Fake St",
    city: "Faketon",
    state: "MA",
    zip: “12345”
  }
  books: [ 27464, 747854, ...]
} 
 
List books = Arrays.asList(27464, 747854);
DBObject person = new BasicDBObject("_id", "jo")
                            .append("name", "Jo Bloggs")
                            .append("address", new BasicDBObject("street", "123 Fake St")
                                                         .append("city", "Faketon")
                                                         .append("state", "MA")
                                                         .append("zip", 12345))
                            .append("books", books);
 
 
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("Examples");
DBCollection collection = database.getCollection("people");
collection.insert(person);
 
 
> use Examples
switched to db Examples
> show collections
people
system.indexes
> _ 
  
 
> db.people.findOne()
{
    "_id" : "jo",
    "name" : "Jo Bloggs",
        "age": 34,
    "address" : {
        "street" : "123 Fake St",
        "city" : "Faketon",
        "state" : "MA",
        "zip" : "12345"
    },
    "books" : [
        27464,
        747854
    ]
}
> _
     
[8]
  
Alternatively 
http://zetcode.com/java/mongodb/
 
Enjoy :) 
 
  



Followers