Example usage for com.mongodb.client MongoCollection findOneAndUpdate

List of usage examples for com.mongodb.client MongoCollection findOneAndUpdate

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection findOneAndUpdate.

Prototype

@Nullable
TDocument findOneAndUpdate(Bson filter, List<? extends Bson> update);

Source Link

Document

Atomically find a document and update it.

Usage

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

/**
 * This method will update a video information
 *
 * @param videoId video Id//from  w  w w  .j  a  va2 s .  c  om
 * @author created by Rashida on 21 October
 */
public String updateVideo(String videoId, String url) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();

    VideoDAO result = mongoCollection.findOneAndUpdate(selectQuery,
            new Document("$set", new Document("url", url)));
    if (result != null) {
        resultEvent.setResponseCode(PropertyProvider.get("Updated"));
        return resultEvent.toString();
    } else {
        resultEvent.setResponseCode(PropertyProvider.get("Null"));
        return resultEvent.toString();
    }
}

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

public String addVideoLike(String videoId, String likeInfo) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();
    Like videolikeInfo = Like.getLikeInfo(likeInfo);
    if (videolikeInfo != null) {
        VideoDAO result = mongoCollection.findOneAndUpdate(selectQuery,
                new Document("$push", new Document("like", JSON.parse(videolikeInfo.toString()))));
        resultEvent.setResponseCode(PropertyProvider.get("Created"));
        return resultEvent.toString();
    } else {/*from www. j a  v  a2  s .  c  o m*/
        resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
        return resultEvent.toString();

    }
}

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

public String deleteVideoLike(String videoId, String likeId) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    Document sQuery = new Document();
    sQuery.put("videoId", videoId);
    sQuery.put("comment.id", likeId);
    Document pQuery = new Document();
    pQuery.put("id", likeId);
    VideoDAO result = mongoCollection.findOneAndUpdate(sQuery,
            new Document("$pull", new Document("like", pQuery)));
    if (result != null) {
        resultEvent.setResponseCode(PropertyProvider.get("Deleted"));
        return resultEvent.toString();
    } else {//from  www .jav a 2  s. c  om
        resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
        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 {/* w w  w.ja  v  a 2  s  .c  om*/
        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 editVideoComment(String videoId, String commentId, String commentInfo) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    Document selectQuery = new Document();
    selectQuery.put("videoId", videoId);
    selectQuery.put("comment.id", commentId);
    VideoDAO result = mongoCollection.findOneAndUpdate(selectQuery,
            new Document("$set", new Document("comment.$.description", commentInfo)));
    if (result != null) {
        resultEvent.setResponseCode(PropertyProvider.get("Updated"));
        return resultEvent.toString();
    } else {/*from  ww  w. j  ava2 s . c o  m*/
        resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
        return resultEvent.toString();
    }

}

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

public String deleteVideoComment(String videoId, String commentId) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    Document sQuery = new Document();
    sQuery.put("videoId", videoId);
    sQuery.put("comment.id", commentId);
    Document pQuery = new Document();
    pQuery.put("id", commentId);
    VideoDAO result = mongoCollection.findOneAndUpdate(sQuery,
            new Document("$pull", new Document("comment", pQuery)));
    if (result != null) {
        resultEvent.setResponseCode(PropertyProvider.get("Deleted"));
        return resultEvent.toString();
    } else {//from   w  ww. ja  v a  2s .c  o m
        resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
        return resultEvent.toString();
    }
}

From source file:net.liaocy.ml4j.nlp.dict.Term.java

private synchronized int getNextSequence(String name) {
    MongoCollection<Document> colCounter = this.db.getCollection("counters");
    Document find = new Document("_id", name);
    Document update = new Document("$inc", new Document("seq", 1));
    Document obj = colCounter.findOneAndUpdate(find, update);
    return obj.getInteger("seq");
}

From source file:org.helm.rest.MongoDB.java

public long SaveRecord(String table, long id, Map<String, String> data) {
    MongoCollection coll = db.getCollection(table);

    Document doc = new Document();
    for (String k : data.keySet()) {
        String v = data.get(k);//from w  w  w  . ja  v a  2s . com
        if (v != null)
            doc.append(k, new org.bson.BsonString(v));
        else
            doc.append(k, new org.bson.BsonNull());
    }

    if (id > 0) {
        Document where = new Document("id", new BsonInt64(id));
        coll.findOneAndUpdate(where, new Document("$set", doc));
    } else {
        id = GetMaxID(table) + 1;
        doc.append("id", new BsonInt64(id));
        coll.insertOne(doc);
    }

    return id;
}