Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

In this page you can find the example usage for com.mongodb MongoClient getDB.

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:dao.PrivatePostDAO.java

public PrivatePostDAO(MongoClient mongo) {
    this.col = mongo.getDB("vidico").getCollection("privatepost");
}

From source file:dao.PublicPostDAO.java

public PublicPostDAO(MongoClient mongo) {
    this.col = mongo.getDB("vidico").getCollection("public");
}

From source file:dao.SearchTopicDao.java

public SearchTopicDao(MongoClient mongo) {

    this.col = mongo.getDB("vidico").getCollection("public");
}

From source file:dao.SearchUserDao.java

public SearchUserDao(MongoClient mongo) {
    this.col = mongo.getDB("vidico").getCollection("users");
    this.col1 = mongo.getDB("vidico").getCollection("Publicpost");
}

From source file:DashboardServices.FetchInstances.java

@GET
@Produces(MediaType.TEXT_PLAIN)//from   ww w . j  a  v  a 2s .c  o  m
public Response getRole(@Context UriInfo info) {
    String projectId = info.getQueryParameters().getFirst("projectId");
    String entityName = info.getQueryParameters().getFirst("entityName");
    System.out.println("projectId" + projectId);
    String finalOutput = "";
    try {
        MongoClient mongoClient;
        try {
            mongoClient = new MongoClient();
            // Now connect to your databases
            DB db = mongoClient.getDB("SSKDatabase");
            //System.out.println("Connect to database successfully");
            DBCollection coll = db.getCollection("insa");

            DBObject match = new BasicDBObject("$match", new BasicDBObject("_id", projectId));
            DBObject unwind = new BasicDBObject("$unwind", "$" + entityName);
            DBObject project = new BasicDBObject("$project", new BasicDBObject("instances", "$" + entityName));

            AggregationOutput output = coll.aggregate(match, unwind, project);
            if (output != null) {
                System.out.println("Ajay worked");
            } else {
                System.out.println("ajay didnt not woek");
            }
            System.out.println("before if condition");
            for (DBObject result : output.results()) {
                System.out.println("hai i reached here");
                System.out.println(result);
                finalOutput += result;
            }
        } catch (Exception e) {
            System.out.println("error");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Response.status(200).entity(finalOutput).build();
}

From source file:DashboardServices.FetchProject.java

/**
 * Retrieves representation of an instance of DashboardServices.LoginServiceResource
 * @return an instance of java.lang.String
 *//*w  ww  .  j av  a 2  s .c  om*/

@POST
@Produces(MediaType.TEXT_PLAIN)
public Response getUsers(@Context UriInfo info) {
    String userId = info.getQueryParameters().getFirst("userId");
    String finalOutput = "";

    try {
        MongoClient mongoClient;

        try {
            mongoClient = new MongoClient();
            // Now connect to your databases
            DB db = mongoClient.getDB("SSKDatabase");
            //System.out.println("Connect to database successfully");
            DBCollection coll = db.getCollection("UserCollection");

            //DBCollection coll2 = db.getCollection("ProjectCollection");

            DBObject match = new BasicDBObject("$match", new BasicDBObject("_id", userId));

            DBObject unwind = new BasicDBObject("$unwind", new BasicDBObject("path", "$projects"));

            DBObject lookup = new BasicDBObject("$lookup",
                    new BasicDBObject("from", "ProjectCollection")
                            .append("localField", "projects.projectIndexId").append("foreignField", "_id")
                            .append("as", "productObjects"));

            DBObject unwind2 = new BasicDBObject("$unwind", "$productObjects");

            DBObject group = new BasicDBObject("$group",
                    new BasicDBObject("_id", "$_id").append("projects", new BasicDBObject("$push", "$projects"))
                            .append("productObjects", new BasicDBObject("$push", "$productObjects")));
            DBObject group2 = new BasicDBObject("$group",
                    new BasicDBObject("_id", "$productObjects.projectName"));

            //System.out.println("Collection mycol selected successfully");

            AggregationOutput output = coll.aggregate(match, unwind, lookup, unwind2, group, group2);

            for (DBObject result : output.results()) {
                System.out.println(result);
                finalOutput += result;
            }

        } catch (Exception e) {
            System.out.println("error");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return Response.status(200).entity(finalOutput).build();

}

From source file:DashboardServices.Fetchrole.java

@POST
@Produces(MediaType.TEXT_PLAIN)//  w ww  . ja  va 2s  . co m
public Response getRole(@Context UriInfo info) {
    String userId = info.getQueryParameters().getFirst("userId");
    String projectname = info.getQueryParameters().getFirst("projectname");
    String finalOutput = "";
    try {
        MongoClient mongoClient;
        try {
            mongoClient = new MongoClient();
            // Now connect to your databases
            DB db = mongoClient.getDB("SSKDatabase");
            //System.out.println("Connect to database successfully");
            DBCollection coll = db.getCollection("ProjectCollection");
            DBObject match = new BasicDBObject("$match", new BasicDBObject("projectName", projectname));
            DBObject lookup = new BasicDBObject("$lookup",
                    new BasicDBObject("from", "UserCollection").append("localField", "_id")
                            .append("foreignField", "projects.projectIndexId").append("as", "productObjects"));
            DBObject unwind = new BasicDBObject("$unwind", "$productObjects");
            DBObject match2 = new BasicDBObject("$match", new BasicDBObject("productObjects._id", userId));
            DBObject unwind2 = new BasicDBObject("$unwind", "$productObjects.projects");
            DBObject match3 = new BasicDBObject("$match",
                    new BasicDBObject("productObjects.projects.projectName", projectname));
            DBObject project = new BasicDBObject("$project",
                    new BasicDBObject("productObjects.projects.projectRole", 1).append("_id", 0));
            DBObject group = new BasicDBObject("$group",
                    new BasicDBObject("_id", "$productObjects.projects.projectRole"));
            AggregationOutput output = coll.aggregate(match, lookup, unwind, match2, unwind2, match3, project,
                    group);
            for (DBObject result : output.results()) {
                System.out.println(result);
                finalOutput += result;
            }
        } catch (Exception e) {
            System.out.println("error");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Response.status(200).entity(finalOutput).build();
}

From source file:DashboardServices.LoginService.java

/**
 * Retrieves representation of an instance of DashboardServices.LoginService
 * @return an instance of java.lang.String
 *//*  w ww . j  a  v  a  2  s. c  o m*/

@POST
@Produces(MediaType.TEXT_PLAIN)
public Response getUsers(@Context UriInfo info) {
    String username = info.getQueryParameters().getFirst("username");
    String password = info.getQueryParameters().getFirst("password");
    MongoClient mongoClient;
    String finalOutput = "HELLO";
    try {
        mongoClient = new MongoClient();
        // Now connect to your databases
        DB db = mongoClient.getDB("SSKDatabase");
        DBCollection coll = db.getCollection("UserCollection");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("_id", username));
        obj.add(new BasicDBObject("password", password));
        andQuery.put("$and", obj);
        //System.out.println(andQuery.toString());
        DBCursor cursor = coll.find(andQuery);

        while (cursor.hasNext()) {
            finalOutput += cursor.next();
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (finalOutput.equals("HELLO")) {

        return Response.status(200).entity("failure").build();
    } else {
        return Response.status(200).entity("sucess").build();
    }

}

From source file:DashboardServices.LoginServiceResource.java

/**
 * Retrieves representation of an instance of DashboardServices.LoginServiceResource
 * @return an instance of java.lang.String
 *///from   w  w  w.  j a  v  a 2  s .co m

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getUsers(@Context UriInfo info) {
    String username = info.getQueryParameters().getFirst("username");
    String password = info.getQueryParameters().getFirst("password");
    MongoClient mongoClient;
    String finalOutput = "HELLO";
    try {
        mongoClient = new MongoClient();
        // Now connect to your databases
        DB db = mongoClient.getDB("SSKDatabase");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("UserCollection");
        System.out.println("Collection mycol selected successfully");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("_id", username));
        obj.add(new BasicDBObject("password", password));
        andQuery.put("$and", obj);
        System.out.println(andQuery.toString());
        DBCursor cursor = coll.find(andQuery);
        if (cursor != null) {
            System.out.println("hai");
        }
        while (cursor.hasNext()) {
            System.out.println("Entered Loop");
            finalOutput += cursor.next();
        }

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (finalOutput.equals("HELLO")) {
        System.out.println("abshah");
        return Response.status(200).entity("failure").build();
    } else {
        return Response.status(200).entity("sucess").build();
    }

}

From source file:DataAccess.DAO.ItemDAO.java

public Boolean connectDB() {
    try {/*from  www. java  2s . c om*/
        // To connect to mongo dbserver
        MongoClient mongoClient = new MongoClient(Constants.HOST_BD_MONGO, Constants.PORT_BD_MONGO);
        // Now connect to your databases
        DB db = mongoClient.getDB("recuerdos");
        System.out.println("Connect to database successfully");
        coll = db.getCollection("items");
        System.out.println("Collection account selected successfully");
        return true;
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        return false;
    }
}