MongoDB – NoSQL

Its document-oriented NoSQL database by 10gen.
It is scalable, open source, high-performance document oriented DB.
New nodes can be added to NoSQL databases. Which means we can achieve horizontal scalability (linearly proposal to performance. More DB's -> more performance)

NoSQL database types:

Screen Shot 2017-04-01 at 8.58.50 PM


Pros:
– Query language
– Fast performance
– Horizontal scalability

Cons:
– No join support
– No complex transactions support
– No constraints support

Entity can be represented by collections(table)
Its stores documents(objects, row) with in a collection(table) in BSON format
   var mydoc = {
               _id: ObjectId("5099803df3f4948bd2f98391"),  <- mandatory
               name: { first: "Alan", last: "Turing" },
               birth: new Date('Jun 23, 1912'),
               death: new Date('Jun 07, 1954'),
               contribs: [ “Turing machine”, “Turing test”, “Turingery” ],
               views : NumberLong(1250000)
            }
   The above fields have the following data types:

    _id holds an ObjectId.
    name holds an embedded document that contains the fields first and last.
    birth and death hold values of the Date type.
    contribs holds an array of strings.
    views holds a value of the NumberLong type.         

Document Oriented Query Language: Wild cards can be used.
  db.employess.find({_id:1234});
  db.employees.find({name:"Hasan"});
  db.employees.find().sort({name:1});

Adhoc Queries: Support search by field, range queries, and regular expression searches.
Indexing support : Any field in a document can be indexed.
Replication support: supports master-slave replication
   – A master can perform reads and writes.
   – A slave can perform master and can only be used for reads and backup.
Duplication data: Runs over multiple servers. The data is duplicated to keep the system running on hardware failures.
Load balancing: (Automatic)
Scalable: New nodes can be provisioned against running DB. Scales horizontally.
Capped collection: circular queue.
File storage: MongoDB can be used as a file system, taking advantage of load balancing & data replication features. Special feature GridFS available for this purpose.
Aggregation: MapReduce can be used for batch processing of data and aggregation operations. Offers similar functionalities as SQL Group by clause.
Server side javascript execution: java script can be used in queries.
– Location support
– No conversion required between OOPL & MongoDB.

Basic Queries:  (RoboMOngo -> Groovy Adminstrative UI)

1. create the database -> use cricketleague
2. create a table(collections)-> db.createCollection("Teams");
3. list a collection(table) with in DB -> show collections
4. drop a collection -> db.Teams.drop();

5. insert data into collection ->
    a={"name":"siva", "conference":"America"}
    db.Teams.save(a);
6. query data from collection ->
    db.Teams.find("name":"NY");
7. multiple update a document(row) with in a collection ->
    db.Teams.update({"name":"NY" },{"name":"XY"},{multi:true});
8. single update a document(row) with in a collection ->
    db.Teams.update({_id:1010,"name":"NY"});
    db.Teams.remove({_id:1001});
9. Bulk load or script data in to the collection
    load(<"file path">);
10. db.CopyDatabase("cricketLeague", "Football League", "localhost");
11. Aggregator:
        db.users.aggregate({
           $group:{
             _id:"eyecolor",
             total:{$sum:1}
           }
        });
        db.users.aggregate({
           $group:{
              _id:"$gender",
              avgAge:{$avg:"$age"}
           }
        })
12. db.users.ensureIndex({"age":1})
13. db.users.getIndexes()


Spring MongoDB integration:

.MongoDBFactory
.SimpleMongoDBFactory
.MongoOperations

@Bean
public MongoDbFactory mongoDbFactory(){
    return new MongoClient(MONGO_HOST, MONGO_PORT)
}

@Bean
public MongoOperations mongoOperations(){
    return new MongoTemplate(mongoDBFactory());
}

-Define your document
@Document(collections="simpleDocument") 
public class SimpleDocument{
    @Id
    String id;
    String name;
}

SimpleDocument s1 = new SimpleDocument("1","Siva");
mongoOperations.save(s1);

 

Leave a Reply

Your email address will not be published. Required fields are marked *