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:com.shampan.model.VideoModel.java

public String getVideoLikeList(String videoId) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();
    Document pQuery = new Document();
    pQuery.put("like", "$all");
    VideoDAO videoLikeList = mongoCollection.find(selectQuery).projection(pQuery).first();
    if (videoLikeList != null) {
        return videoLikeList.toString();
    } else {//from w  w w.  ja v a  2 s  .  co m
        resultEvent.setResponseCode(PropertyProvider.get("Null"));
        return resultEvent.toString();
    }
}

From source file:com.shampan.model.VideoModel.java

public String addVideoComment(String videoId, String commentInfo) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();
    Comment videoCommentInfo = Comment.getCommentInfo(commentInfo);
    try {// www  .  j  ava  2  s  .  com
        if (videoCommentInfo != null) {
            VideoDAO result = mongoCollection.findOneAndUpdate(selectQuery,
                    new Document("$push", new Document("comment", JSON.parse(videoCommentInfo.toString()))));
            if (result != null) {
                resultEvent.setResponseCode(PropertyProvider.get("Created"));
                return resultEvent.toString();
            }
        }
    } catch (NullPointerException npe) {
        LogWriter.getErrorLog().error("null value exception");
    }
    resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
    return resultEvent.toString();
}

From source file:com.shampan.model.VideoModel.java

public String getVideoComments(String videoId) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();
    Document pQuery = new Document();
    pQuery.put("comment", "$all");
    VideoDAO videoCommentList = mongoCollection.find(selectQuery).projection(pQuery).first();
    if (videoCommentList != null) {
        return videoCommentList.toString();
    } else {//from   w w  w  .j a  v  a  2s . c o m
        resultEvent.setResponseCode(PropertyProvider.get("Null"));
        return resultEvent.toString();
    }
}

From source file:com.stratio.deep.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Calculate splits./*from  ww  w.  jav a  2  s. c o  m*/
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private DeepPartition[] calculateSplits(DBCollection collection) {

    BasicDBList splitData = getSplitData(collection);
    List<ServerAddress> serverAddressList = collection.getDB().getMongo().getServerAddressList();

    if (splitData == null) {
        Pair<BasicDBList, List<ServerAddress>> pair = getSplitDataCollectionShardEnviroment(
                getShards(collection), collection.getDB().getName(), collection.getName());
        splitData = pair.left;
        serverAddressList = pair.right;
    }

    Object lastKey = null; // Lower boundary of the first min split

    List<String> stringHosts = new ArrayList<>();

    for (ServerAddress serverAddress : serverAddressList) {
        stringHosts.add(serverAddress.toString());
    }
    int i = 0;

    MongoPartition[] partitions = new MongoPartition[splitData.size() + 1];

    for (Object aSplitData : splitData) {

        BasicDBObject currentKey = (BasicDBObject) aSplitData;

        Object currentO = currentKey.get(MONGO_DEFAULT_ID);

        partitions[i] = new MongoPartition(mongoDeepJobConfig.getRddId(), i,
                new DeepTokenRange(lastKey, currentO, stringHosts), MONGO_DEFAULT_ID);

        lastKey = currentO;
        i++;
    }
    QueryBuilder queryBuilder = QueryBuilder.start(MONGO_DEFAULT_ID);
    queryBuilder.greaterThanEquals(lastKey);
    partitions[i] = new MongoPartition(0, i, new DeepTokenRange(lastKey, null, stringHosts), MONGO_DEFAULT_ID);
    return partitions;
}

From source file:com.stratio.deep.mongodb.reader.MongoReader.java

License:Apache License

/**
 * Create query partition./*w  ww  .  j  av  a 2s  .  co  m*/
 *
 * @param partition the partition
 * @return the dB object
 */
private DBObject createQueryPartition(MongoPartition partition) {

    QueryBuilder queryBuilderMin = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMin = queryBuilderMin.greaterThanEquals(partition.splitWrapper().getStartToken()).get();

    QueryBuilder queryBuilderMax = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMax = queryBuilderMax.lessThan(partition.splitWrapper().getEndToken()).get();

    QueryBuilder queryBuilder = QueryBuilder.start();
    if (partition.splitWrapper().getStartToken() != null) {
        queryBuilder.and(bsonObjectMin);
    }

    if (partition.splitWrapper().getEndToken() != null) {
        queryBuilder.and(bsonObjectMax);
    }

    LOG.debug("mongodb query " + queryBuilder.get());

    return queryBuilder.get();
}

From source file:com.vaushell.gfmongodb.MongoDbUserRealm.java

License:Open Source License

@Override
public Enumeration getGroupNames(final String username) {
    final QueryBuilder builder = QueryBuilder.start(getProperty(PARAM_USERNAME)).is(username);
    final DBObject user = usersCollection.findOne(builder.get());
    if (user == null) {
        return null;
    }/* w ww.  j a va 2  s .  c om*/

    return Collections.enumeration(getGroups(user));
}

From source file:com.vaushell.gfmongodb.MongoDbUserRealm.java

License:Open Source License

/**
 * Authenticate user./*from w  w w  .  jav a 2  s. c o  m*/
 *
 * @param username Username.
 * @param givenPassword Password
 * @return List of groups.
 * @throws LoginException
 */
String[] authenticate(final String username, final char[] givenPassword) throws LoginException {
    if (username == null || username.length() <= 0 || givenPassword == null || givenPassword.length <= 0) {
        throw new LoginException("username or password is empty");
    }

    final QueryBuilder builder = QueryBuilder.start(getProperty(PARAM_USERNAME)).is(username);
    final DBObject user = usersCollection.findOne(builder.get());
    if (user == null) {
        throw new LoginException("cannot find user with username '" + username + "'");
    }

    final String databasePassword = (String) user.get(getProperty(PARAM_PASSWORD));
    if (databasePassword == null || databasePassword.length() <= 0) {
        throw new LoginException("cannot find nonempty password for username '" + username + "'");
    }

    final String transformedPassword = DigestUtils.sha256Hex(new String(givenPassword));
    if (!databasePassword.equals(transformedPassword)) {
        throw new LoginException("password is wrong for username '" + username + "'");
    }

    final List<String> groups = getGroups(user);
    return groups.toArray(new String[groups.size()]);
}

From source file:course.week3.BlogPostDAO.java

License:Apache License

public DBObject findByPermalink(String permalink) {

    DBObject post = null;/*from   ww  w.  jav  a  2  s  . c  o  m*/
    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    // XXX HW 3.2,  Work Here
    post = postsCollection.findOne(query);

    return post;
}

From source file:course.week3.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://from   www .j  a  v a 2s.c  o m
    // - 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
    BasicDBObject object = new BasicDBObject("author", name).append("body", body);
    if (email != null && !email.isEmpty()) {
        object.append("email", email);
    }

    DBObject query = QueryBuilder.start("permalink").is(permalink).get();
    DBObject update = new BasicDBObject("$push", new BasicDBObject("comments", object));

    postsCollection.update(query, update);

}

From source file:field_sum.Field_sum.java

/**
 * @param args the command line arguments
 * @throws java.net.UnknownHostException
 *//*  w w w .  j av  a2  s.  c  o  m*/
public static void main(String[] args) throws UnknownHostException {

    MongoClient client = new MongoClient("localhost", 27017);
    DB myDB = client.getDB("m101");
    DBCollection collection = myDB.getCollection("Sample3");

    /**
     * random_number field generates random numbers 
    */
    Random random_numbers = new Random();

    collection.drop();

    DBObject query = QueryBuilder.start("x").greaterThan(10).lessThan(70).and("y").greaterThan(10).lessThan(70)
            .get();

    for (int i = 0; i < 100; i++) {
        collection.insert(new BasicDBObject("x", random_numbers.nextInt(100))
                .append("y", random_numbers.nextInt(100)).append("z", random_numbers.nextInt(200)));
    }

    DBCursor cursor = collection.find(query);
    int sum = 0;

    try {
        while (cursor.hasNext()) {
            DBObject dBObject = cursor.next();
            sum += (int) dBObject.get("x");
            System.out.println(dBObject.get("x"));
        }
    } finally {
        cursor.close();
    }
    System.out.println(sum);

}