Example usage for com.mongodb DBCollection update

List of usage examples for com.mongodb DBCollection update

Introduction

In this page you can find the example usage for com.mongodb DBCollection update.

Prototype

public WriteResult update(final DBObject query, final DBObject update) 

Source Link

Document

Modify an existing document.

Usage

From source file:com.mycompany.bean.PlytaService.java

public String updateDocSingle(String tytulBase, ArrayList<String> single) {
    try {/*ww  w  .  j  a  v  a2 s.c  o  m*/
        DBCollection collection = getConnection("PlytyDB", "plyty");
        collection.update(new BasicDBObject("tytul", tytulBase),
                new BasicDBObject("$set", new BasicDBObject("single", single)));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad podczas edycji plyty");
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return "Edycja wykonana";
}

From source file:com.mycompany.bean.PlytaService.java

public String updateDocNagrody(String tytulBase, ArrayList<String> nagrody) {
    try {/*from  w  w  w.  j  a v a  2 s  .c o  m*/
        DBCollection collection = getConnection("PlytyDB", "plyty");
        collection.update(new BasicDBObject("tytul", tytulBase),
                new BasicDBObject("$set", new BasicDBObject("nagrody", nagrody)));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad podczas edycji plyty");
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return "Edycja wykonana";
}

From source file:com.mycompany.bean.PlytaService.java

public String updateDocTytul(String tytulBase, String tytulNew) {
    try {//from   w ww.j a v  a  2  s.c  om
        DBCollection collection = getConnection("PlytyDB", "plyty");
        collection.update(new BasicDBObject("tytul", tytulBase),
                new BasicDBObject("$set", new BasicDBObject("tytul", tytulNew)));
    } catch (Exception e) {
        log.log(Level.SEVERE, "Blad podczas edycji plyty");
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return "Edycja wykonana";
}

From source file:com.mycompany.model.MarkerDAOImpl.java

@Override
public void saveOrUpdate(Marker marker) {
    DBCollection markerCollection = MongoConfig.getMongoConnection().getCollection("markers");
    BasicDBObject document = new BasicDBObject();
    document.put("lat", marker.getLat());
    document.put("lng", marker.getLng());
    document.put("title", marker.getTitle());
    document.put("icon", marker.getIcon());
    document.put("content", marker.getContent());

    if (marker.getId().length() > 0) {

        BasicDBObject query = new BasicDBObject();
        query.append("_id", new ObjectId(marker.getId()));
        markerCollection.update(query, document);
    } else {//from www. ja  v a  2  s.c  o  m
        markerCollection.insert(document);
    }
}

From source file:com.mythesis.userbehaviouranalysis.ProfileAnalysis.java

License:Apache License

/**
 * a method that stores the query that has been suggested by the user
 * @param crawlerOutputPath SWebRank output directory used to check if a relevant query already exists
 * @param profile the query's relevant profile
 * @param query the given query//from   w w w.j  a  va 2 s .co m
 */
public void storeQuery(String crawlerOutputPath, String profile, String query) {

    System.out.println(crawlerOutputPath);
    System.out.println(profile);
    System.out.println(query);
    //Find output paths
    File root = new File(crawlerOutputPath);
    File[] contents = root.listFiles();
    List<String> sWebRanklevels = new ArrayList<>();
    for (File f : contents) {
        if (f.getAbsolutePath().contains("level"))
            sWebRanklevels.add(f.getAbsolutePath());
    }

    //Find all query paths
    List<String> queries = new ArrayList<>();
    for (String s : sWebRanklevels) {
        File level = new File(s);
        File[] queriesFiles = level.listFiles();
        for (File f : queriesFiles) {
            if (!f.getAbsolutePath().contains("txt")) {
                String str = f.getAbsolutePath();
                queries.add(str.substring(str.lastIndexOf("\\") + 1).replace("-query", "").replace("+", " "));
            }
        }
    }

    //check if a relevant query already exists - I use Jaro-Winkler distance
    query = query.trim().replaceAll(" +", " ");
    for (String q : queries) {
        JaroWinklerDistance jwd = new JaroWinklerDistance();
        double distance = jwd.getDistance(q, query);
        if (distance > 0.9) { // threshold = 0.9
            return;
        }
    }

    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("profileAnalysis");

    DBCollection DBqueries = db.getCollection("newQueries");
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("profile", profile);
    DBObject document = DBqueries.findOne(searchQuery);
    boolean flag = false;

    //check if a relevant query exists in the database - I use Jaro-Winkler distance
    if (document != null) {
        flag = true;
        BasicDBList storedQueries = (BasicDBList) document.get("queries");
        for (Object quer : storedQueries) {
            JaroWinklerDistance jwd = new JaroWinklerDistance();
            double distance = jwd.getDistance((String) quer, query);
            if (distance > 0.9) { // threshold = 0.9
                return;
            }
        }
    }

    //if document already exists add the new query
    if (flag) {
        DBqueries.update(searchQuery, new BasicDBObject("$push", new BasicDBObject("queries", query)));
    } else { //otherwise create a new document
        BasicDBList dbl = new BasicDBList();
        dbl.add(query);
        BasicDBObject entry = new BasicDBObject("profile", profile).append("queries", dbl);
        DBqueries.insert(entry);
    }
}

From source file:com.nowellpoint.mongodb.persistence.impl.DocumentManagerImpl.java

License:Apache License

@Override
public <T> T merge(T document) {
    doumentManagerFactory.prePersist(document);
    String collectionName = doumentManagerFactory.resolveDocumentName(document.getClass());
    DBCollection collection = getDB().getCollection(collectionName);
    Object documentId = doumentManagerFactory.resolveId(document);
    DBObject dbObject = doumentManagerFactory.convertObjectToDocument(document);
    DBObject query = new BasicDBObject(DocumentManagerFactoryImpl.ID, documentId);
    WriteResult result = collection.update(query, dbObject);
    if (result.getError() != null) {
        throw new MongoException(result.getLastError());
    }/*from   w  w  w.  j  av a 2  s .c  o m*/
    doumentManagerFactory.postPersist(document);
    return document;
}

From source file:com.redhat.lightblue.metadata.mongo.MetadataCache.java

License:Open Source License

/**
 * Update the collection version in db, and invalidate cache
 *//*from w w  w .  j  a  va2s.  c  o  m*/
public synchronized void updateCollectionVersion(DBCollection collection) {
    BasicDBObject query = new BasicDBObject(MongoMetadata.LITERAL_ID, LITERAL_COLL_VER);
    BasicDBObject update = new BasicDBObject("$inc", new BasicDBObject(LITERAL_COLL_VER, 1));
    int nUpdated;
    try {
        WriteResult r = collection.update(query, update);
        nUpdated = r.getN();
    } catch (Exception e) {
        nUpdated = 0;
    }
    if (nUpdated == 0) {
        // Try to ins
        BasicDBObject doc = new BasicDBObject(MongoMetadata.LITERAL_ID, LITERAL_COLL_VER);
        doc.put(LITERAL_COLL_VER, 0l);
        try {
            collection.insert(doc);
        } catch (Exception e) {
        }
    }
    cache.clear();
}

From source file:com.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * Common handler for updating existing objects. Assures that errors are handled in a
 * consistent way./*  w ww  .j  av a 2s.  co  m*/
 * 
 * @param collection
 * @param object
 * @throws SiteWhereException
 */
public static void update(DBCollection collection, DBObject query, DBObject object) throws SiteWhereException {
    WriteResult result = collection.update(query, object);
    if (!result.getLastError().ok()) {
        throw new SiteWhereException("Error during update: " + result.getLastError().toString());
    }
}

From source file:com.softlyinspired.jlw.connections.dbConnection.java

License:Open Source License

public int updateExisting() {
    /* Update connection details */

    int result = 0;
    DBCollection connectionColl = repoConnection.getConnectionCollection();

    checkExisting();//from w w  w .  j a v  a2  s  .  c  o  m

    try {
        // search for the id
        BasicDBObject updateQuery = new BasicDBObject();
        BasicDBObject searchQuery = new BasicDBObject().append("name", connectionName);
        DBObject changes = new BasicDBObject().append("driver", connectionDriver).append("url", connectionURL)
                .append("user", connectionUser).append("password", connectionPassword);
        updateQuery.append("$set", changes);
        connectionColl.update(searchQuery, updateQuery);
        JLWUtilities.scriptInfoMessage("Existing Connection Saved");
    } catch (Exception e) {
        System.out.println("Error updating connection");
    }

    return result;
}

From source file:com.softlyinspired.jlw.reports.ReportReference.java

License:Open Source License

public int updateExisting() {
    /* Update report details */

    int result = 0;
    DBCollection coll = repoConnection.getReportsCollection();
    checkExisting();//from  w w  w  .j av  a2 s. com

    try {
        // search for the id
        BasicDBObject updateQuery = new BasicDBObject();

        BasicDBObject searchQuery = new BasicDBObject().append("reportId", reportId);
        DBObject changes = new BasicDBObject().append("name", reportName).append("type", reportType)
                .append("description", reportDescription).append("category", reportCategory)
                .append("reportLocation", reportLocation);
        updateQuery.append("$set", changes);
        coll.update(searchQuery, updateQuery);
    } catch (Exception e) {
        System.out.println("Error updating report");
    }
    return result;
}