Aggregation & Indexing With Suitable Example Using MongoDB. | I'M FROSTY
Make some Money!

What's Hot

recentposts

Aggregation & Indexing With Suitable Example Using MongoDB.

============ Aggregation & JAVA Program In MongoDB ============

import java.net.UnknownHostException;
import java.util.regex.Pattern;
import com.mongodb.*;

public class ManagePeople {
    
    public static void main(String[] args) {
        try {
            Mongo m = new Mongo("localhost", 27017);
            DB db = m.getDB("sampledb");
            DBCollection coll = db.getCollection("people");
            DBCursor cur = coll.find();
            while (cur.hasNext())
            coll.remove(cur.next());
            coll.ensureIndex(new BasicDBObject("id", 

1).append("unique", true));

            coll.createIndex(new BasicDBObject("name", 1));

            coll.insert(makePersonDocument(6655, "James", "male"));

            coll.insert(makePersonDocument(6797, "Bond", "male")); 

            coll.insert(makePersonDocument(6643, "Cheryl", "female"));

            coll.insert(makePersonDocument(7200, "Scarlett", "female"));

            coll.insert(makePersonDocument(6400, "Jacks", "male"));

            System.out.println("Total Records : " + coll.getCount());



            cur = coll.find();

            printResults(cur, "Find All Records");

            

            cur = coll.find(new BasicDBObject("id", 6655));

            printResults(cur, "Find id = 6655");

            

            cur = coll.find(new BasicDBObject()

            .append("id", new BasicDBObject("$lte", 6700)));

        printResults(cur, "Find id <= 6700");

        

        cur = coll.find(new BasicDBObject()

        .append("id", new BasicDBObject("$lte", 6700))

        .append("gender", "male"));

        printResults(cur, "Find id <= 6700 and gender = male");

        

        cur = coll.find(new BasicDBObject()

        .append("name", Pattern.compile("^ja.*?s$", Pattern.CASE_INSENSITIVE)))

        .sort(new BasicDBObject("name", -1));

        

        printResults(cur, "Find name like Ja%s and sort reverse by name");

        

        cur = coll.find(new BasicDBObject()

        .append("gender", "female"))

        .sort(new BasicDBObject("id", -1))

        .limit(2);

        printResults(cur, "Get top 2 (by id) ladies");

        cur  = coll.find();

        while(cur.hasNext()) {

            BasicDBObject set = new BasicDBObject("$inc", new BasicDBObject("id", -10));

            

            if ("male".equals(cur.next().get("gender")))

                set.append("$set", new BasicDBObject("name", "Sir ".concat((String) cur.curr().get("name"))));

            else 

                set.append("$set", new BasicDBObject("name", "Mme ".concat((String) cur.curr().get("name"))));



            coll.update(cur.curr(), set);            

        }

        cur  = coll.find();

        printResults(cur, "All, after id and name update");

        

        }

            

    catch (UnknownHostException ex) {

    ex.printStackTrace();

    

    }

    catch (MongoException ex) {

    ex.printStackTrace();

    }

}

        private static void printResults(DBCursor cur, String message) {

            System.out.println("<<<<< " + message + " >>>>>");

            while (cur.hasNext()) {

                System.out.println(cur.next().get("id") + "," + cur.curr().get("name") + "," + cur.curr().get("gender"));

            }

        }

        private static BasicDBObject makePersonDocument(int id, String name, String gender) {

            BasicDBObject doc = new BasicDBObject();

            doc.put("id", id);

            doc.put("name", name);

            doc.put("gender", gender);

            return doc;                           

        }

============ OUTPUT ============
7200,Scarlett,female
6400,Jacks,male
<<<<<<<<<< Find id = 6655 >>>>>>>>>>>>
6655,James,male
<<<<<<<<<< Find id <= 6700 >>>>>>>>>>>>
6400,Jacks,male
6643,Cheryl,female
6655,James,male
<<<<<<<<<< Find id <= 6700 and gender = male >>>>>>>>>>>>
6400,Jacks,male
6655,James,male
<<<<<<<<<< Find name like Ja%s and sort reverse by name >>>>>>>>>>>>
6655,James,male
6400,Jacks,male
<<<<<<<<<< Get top 2 (by id) ladies >>>>>>>>>>>>
7200,Scarlett,female
6643,Cheryl,female
<<<<<<<<<< All, after id and name update >>>>>>>>>>>>
6645,Sir James,male
6633,Mme Cheryl,female
7190,Mme Scarlett,female
6787,Sir Bond,male
6390,Sir Jacks,male
Aggregation & Indexing With Suitable Example Using MongoDB. Reviewed by Hardik Pandya on 3:08:00 PM Rating: 5

No comments:

All Rights Reserved by I'M FROSTY © 2014 - 2016
*The Content Is Copyrighted To & May Not Be Copied / Republished.

Contact Form

Name

Email *

Message *

Powered by Blogger.