package mongo;

import com.mongodb.*;
import com.mongodb.util.JSON;
import org.bson.types.ObjectId;

import java.net.UnknownHostException;

public class MongoManager {
    private MongoClient mongoClient = null;
    private DBCollection results = null;
    private DB db = null;
    private static MongoDbConfig mongoConfig = new MongoDbConfig();

    public MongoManager() {
        this(mongoConfig.getIp());
    }


    public MongoManager(String ip) {
        this(ip, mongoConfig.getPort());
    }

    public MongoManager(String ip, int port) {
        try {
            mongoClient = new MongoClient(ip, port);
            System.out.println("Connection was established!");
            db = mongoClient.getDB("monitoringResults");
            results = db.getCollection("testData");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    /**
     * Save a json in the DB.
     *
     * @param jsonString the json to be saved in the db
     * @return the ID of the saved object
     */
    public String pushJson(String jsonString) {
        Object jsonObj = JSON.parse(jsonString);
        DBObject dbObj = (DBObject)jsonObj;
        results.insert(dbObj);
        return dbObj.get("_id").toString();
    }

    /**
     * Query for a Json using id.
     *
     * @param id id of the record in db
     * @return Json with the db data for the provided id
     */
    public String pullJsonById(String id) {
        BasicDBObject query = new BasicDBObject();
        query.put("_id", new ObjectId(id));
        DBObject dbObj = results.findOne(query);
        return dbObj.toString();
    }

    public void closeConnection() {
        if (mongoClient!= null) {
            mongoClient.close();
            System.out.println("Connection was closed!");
        }
    }

}

@@ -1624,32 +1624,445 @@
 tring();%0A    %7D%0A%0A
+    public void updateJsonWithId(String id, String field,  String value) %7B%0A        BasicDBObject query = new BasicDBObject();%0A        query.put(%22_id%22, new ObjectId(id));%0A%0A        BasicDBObject updateDocument = new BasicDBObject();%0A        updateDocument.append(%22$set%22, new BasicDBObject().append(field, value));%0A%0A        results.update(query, updateDocument);%0A    %7D%0A%0A    /**%0A     * Close connection to DB%0A     */%0A
     public void 

Add update method for mongoDB