Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:at.tuwien.aic.Main.java

private static void classifyTopic() {
    String topic = getNonEmptyString("Enter a topic you want to query");
    Mongo mongo;/*from   w  w w  . ja  v  a2  s .co  m*/
    DB db = null;

    try {
        mongo = new Mongo(_prop.getProperty("db_host"));
        db = mongo.getDB(_prop.getProperty("db_name"));
    } catch (UnknownHostException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }

    Classifier c = ClassifyTweet.loadModel("resources/classifier.model");
    TweetScorer scorer = new TweetScorer();

    DBCollection tweetCollection = db.getCollection("tweets");

    Pattern pattern = Pattern.compile("^.+" + topic + ".+$");
    DBObject query = QueryBuilder.start("text").regex(pattern).get();
    DBCursor resultSet = tweetCollection.find(query);

    int count = 0;
    double value = 0;
    double tweetClassifiedScore = 0;
    double tweetPosUserScore = 0;
    double tweetNegUserScore = 0;

    while (resultSet.hasNext()) {
        try {
            DBObject obj = resultSet.next();
            String tweetText = (String) obj.get("text");

            tweetClassifiedScore += ClassifyTweet.classifyTweet(c, tweetText);
            double score = scorer.scoreTweet(obj);

            if (tweetClassifiedScore > 0) {
                tweetPosUserScore += score;
            } else {
                tweetNegUserScore += score;
            }
            ++count;
        } catch (NumberFormatException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Normalizing between 0 an 1
    value = tweetPosUserScore / (tweetPosUserScore + tweetNegUserScore);
    System.out.println("This topic has a sentiment value of: " + value);
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Theme getThemeByExactZCoordinate(int zcoord) {
    DBObject query = QueryBuilder.start("minZ").lessThanEquals(zcoord).and("maxZ").greaterThanEquals(zcoord)
            .get();/*  ww w  . ja  v  a2s .  c  o  m*/
    try {
        Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(
                ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query).toString(), Theme.class);
        theme.fetchLots();
        return theme;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Theme getThemeByLocation(Location loc) {
    DBObject query = QueryBuilder.start("minZ").lessThanEquals(loc.getBlockZ()).and("maxZ")
            .greaterThanEquals(loc.getBlockZ()).get();
    try {/*from   www  . j  ava  2 s . co m*/
        DBObject themeo = ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query);
        if (themeo != null) {
            Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(themeo.toString(), Theme.class);
            theme.fetchLots();
            return theme;
        }
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
        return null;
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Theme getThemeById(ObjectId id) {
    DBObject query = QueryBuilder.start("_id").is(id).get();
    try {//from www.j  a v a2  s. c o  m
        Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(
                ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query).toString(), Theme.class);
        theme.fetchLots();
        return theme;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Theme getThemeByName(String name) {
    DBObject query = QueryBuilder.start("name").is(name).get();
    try {//from ww w . j a va  2s. c  o  m
        DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getThemeCollection().find(query);
        DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next();
        Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Theme.class);
        theme.fetchLots();
        return theme;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Lot getLot(String playerUUID) {
    DBObject query = QueryBuilder.start("owner").is(playerUUID).get();
    try {//w w w . j a  v a  2  s .  c o m
        DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getLotCollection().find(query);
        DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next();
        Lot lot = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Lot.class);
        lot.generateBounds();
        lotCache.put(lot.getCorner(), lot);
        return lot;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static Lot getLot(String playerUUID, ObjectId themeId) {
    DBObject query = QueryBuilder.start("owner").is(playerUUID).and("belongsToTheme").is(themeId).get();
    try {//from  w  w w  .  j a  v a  2  s. c o m
        DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getLotCollection().find(query);
        DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next();
        Lot lot = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Lot.class);
        lot.generateBounds();
        lotCache.put(lot.getCorner(), lot);
        return lot;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:co.mcme.themedbuilds.database.DatabaseUtil.java

License:Open Source License

public static PlayerReport getPlayerReport(OfflinePlayer p) {
    DBObject query = QueryBuilder.start("uuid").is(p.getUniqueId().toString()).get();
    try {//from  ww  w . ja v  a  2 s.  c o  m
        DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getPlayerCollection().find(query);
        DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next();
        PlayerDocument doc = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(),
                PlayerDocument.class);
        PlayerReport report = new PlayerReport(p, doc.getLots(), doc.getLots().get(doc.getLots().size() - 1));
        return report;
    } catch (IOException ex) {
        ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java

License:Apache License

public DBObject findByPermalink(String permalink) {

    DBObject post = null;/* w w  w .  j  a v a 2s. co  m*/
    // XXX HW 3.2,  Work Here
    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    post = postsCollection.find(query).next();

    return post;
}

From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java

License:Apache License

public void addPostComment(final String name, final String email, final String body, final String permalink) {

    // XXX HW 3.3, Work Here
    // Hints:/* w w w .  ja v  a2 s .  com*/
    // - email is optional and may come in NULL. Check for that.
    // - best solution uses an update command to the database and a suitable
    //   operator to append the comment on to any existing list of comments
    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    DBObject post = postsCollection.find(query).next();
    BasicDBObject comment = new BasicDBObject();
    comment.append("author", name);
    if (email != null) {
        comment.append("email", email);
    }
    comment.append("body", body);
    post.put("comments", comment);
    postsCollection.update(query, new BasicDBObject("$push", new BasicDBObject("comments", comment)));

}