Example usage for com.mongodb DBCollection save

List of usage examples for com.mongodb DBCollection save

Introduction

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

Prototype

public WriteResult save(final DBObject document, final WriteConcern writeConcern) 

Source Link

Document

Update an existing document or insert a document depending on the parameter.

Usage

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

/**
 * Creates the CC Fields Collection and insert a lilacs metadoc document.
 * @param coll CC Fields Collection//from www .  java2s .co  m
 * @return true if ok, false if error
 */
private static boolean createCcFieldsCollection(final DBCollection coll) {
    assert coll != null;

    final BasicDBObject doc = new BasicDBObject();
    final BasicDBList lst = new BasicDBList();

    lst.add(1);
    lst.add(920);
    lst.add(930);
    doc.put(MST_FIELD, "LILACS");
    doc.put(ID_TAG_FIELD, 2);
    doc.put(URL_TAG_FIELD, 8);
    doc.put(CC_TAGS_FIELD, lst);

    final WriteResult ret = coll.save(doc, WriteConcern.ACKNOWLEDGED);

    return ret.getCachedLastError().ok();
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean saveRecord(final String mstName, final int id, final String url, final String err,
        final int urlTag, final List<Integer> ccsFlds, final Master mst, final DBCollection coll,
        final DBCollection hcoll, final Map<Integer, Map<String, Integer>> occMap)
        throws BrumaException, IOException {
    assert mstName != null;
    assert id > 0;
    assert url != null;
    assert urlTag > 0;
    assert err != null;
    assert ccsFlds != null;
    assert mst != null;
    assert coll != null;
    assert hcoll != null;
    assert occMap != null;

    final Record rec = mst.getRecord(id);
    if (!rec.isActive()) {
        //throw new BrumaException("not active record mfn=" + id);
        System.err.println("WARNING: record[" + id + "] is not active. " + "Ignoring it!");
        return false;
    }//from  w  ww .j ava  2s.  co  m

    final List<Field> urls = rec.getFieldList(urlTag);
    final Date now = new Date();
    Date date;

    Map<String, Integer> fldMap = occMap.get(id);
    if (fldMap == null) {
        fldMap = new HashMap<String, Integer>();
        occMap.put(id, fldMap);
    }

    final int occ = nextOcc(url, urls, fldMap);
    if (occ == -1) {
        System.err.println("url[" + url + "] not found. mfn=" + id);
        //throw new IOException("url[" + url + "] not found. mfn=" + id);                
        return false;
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, id + "_" + occ);
    final boolean ret;
    if (fixedRecently(hcoll, query, now, 60)) {
        ret = false;
    } else {
        final BasicDBObject obj = (BasicDBObject) coll.findOne(query);
        if (obj == null) {
            date = now;
        } else {
            date = obj.getDate(LAST_UPDATE_FIELD);
            if (date == null) {
                date = obj.getDate(DATE_FIELD);
            } else {
                final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
                if (!wr.getCachedLastError().ok()) {
                    //TODO
                }
                date = obj.getDate(DATE_FIELD);
            }
        }
        final String url_d = EncDecUrl.decodeUrl(url);
        final String url_d_l = (url_d.length() >= 900) ? url_d.substring(0, 900) + "..." : url_d;
        final String url_l = (url.length() > 900) ? url.substring(0, 900) + "..." : url;
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, date);
        doc.put(LAST_UPDATE_FIELD, now);
        doc.put(MST_FIELD, mstName);
        doc.put(ID_FIELD, id + "_" + occ);
        doc.put(BROKEN_URL_FIELD, url_l);
        doc.put(PRETTY_BROKEN_URL_FIELD, url_d_l);
        doc.put(MSG_FIELD, err);
        doc.put(CENTER_FIELD, getCCS(rec, ccsFlds));

        final WriteResult wres = coll.save(doc, WriteConcern.ACKNOWLEDGED);
        ret = wres.getCachedLastError().ok();
    }
    return ret;
}

From source file:br.bireme.scl.CopyMongoDb.java

License:Open Source License

private static void copyDB(final String from_host, final String to_host, final String from_db,
        final String to_db, final String from_port, final String to_port, final boolean appendCollections,
        final boolean displayAllIds) throws UnknownHostException, IOException {
    assert from_host != null;
    assert to_host != null;
    assert from_db != null;
    assert to_db != null;
    assert from_port != null;
    assert to_port != null;

    final int MAX_LOOP_SIZE = 15000; // MongoException$CursorNotFound
    final MongoClient fromClient = new MongoClient(from_host, Integer.parseInt(from_port));
    final MongoClient toClient = new MongoClient(to_host, Integer.parseInt(to_port));
    final DB fromDb = fromClient.getDB(from_db);
    final DB toDb = toClient.getDB(to_db);

    if (!appendCollections) {
        toDb.dropDatabase();//from w  w w . j a  v  a2  s  .c  o m
    }

    final Set<String> colls = fromDb.getCollectionNames();

    for (String cname : colls) {
        if (cname.equals("system.indexes")) {
            continue;
        }

        final DBCollection fromColl = fromDb.getCollection(cname);
        final DBCollection toColl = toDb.getCollection(cname);

        DBCursor cursor = fromColl.find();
        int curr = 0;

        System.out.println("Copying collection: " + cname);

        while (cursor.hasNext()) {
            if (curr % MAX_LOOP_SIZE == 0) {
                if (curr > 0) {
                    cursor.close();
                    cursor = fromColl.find().skip(curr);
                    if (!cursor.hasNext()) {
                        throw new IOException("hasNext() failed");
                    }
                }
            }
            final DBObject doc = cursor.next();
            final WriteResult ret = toColl.save(doc, WriteConcern.ACKNOWLEDGED);

            if (!ret.getCachedLastError().ok()) {
                System.err.println("write error doc id=" + doc.get("_id"));
            }
            if (++curr % 1000 == 0) {
                System.out.println("+++" + curr);
            }
            if (displayAllIds) {
                System.out.println(" id=" + doc.get("_id"));
            }
        }
        cursor.close();

        System.out.println();
    }
}

From source file:br.bireme.scl.Gizmo.java

License:Open Source License

Collection<Element> getNotExportedElements(final DBCollection coll, final DBCursor cursor) throws IOException {
    assert coll != null;
    assert cursor != null;

    final Collection<Element> col = new ArrayList<Element>();

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final String id = obj.getString(ID_FIELD);
        final BasicDBList lst = (BasicDBList) obj.get(ELEM_LST_FIELD);
        if (lst == null) {
            throw new NullPointerException("Elem list espected");
        }//from   w w w  . jav  a2s . co m
        final BasicDBObject lelem = (BasicDBObject) lst.get(0);
        if (lelem == null) {
            throw new NullPointerException("Elem element espected");
        }
        if (!lelem.getBoolean(EXPORTED_FIELD)) {
            final Element elem = new Element(id, lelem.getString(BROKEN_URL_FIELD),
                    lelem.getString(PRETTY_BROKEN_URL_FIELD), lelem.getString(FIXED_URL_FIELD),
                    obj.getString(MST_FIELD), lelem.getDate(LAST_UPDATE_FIELD).toString(),
                    lelem.getString(USER_FIELD), null, false);
            col.add(elem);

            lelem.put(EXPORTED_FIELD, true);
            final WriteResult res = coll.save(obj, WriteConcern.ACKNOWLEDGED);

            if (!res.getCachedLastError().ok()) {
                throw new IOException("write doc[" + obj.getString(ID_FIELD) + "] failed");
            }
        }
    }

    return col;
}

From source file:br.bireme.scl.UndoUpdate.java

License:Open Source License

private static void undo(final String host, final int port, final String database, final String fromDate,
        final String docId) throws UnknownHostException, ParseException {
    assert host != null;
    assert port > 0;
    assert database != null;
    assert (fromDate != null || docId != null);

    final MongoClient client = new MongoClient(host, port);
    final DB db = client.getDB(database);
    final DBCollection from_coll = db.getCollection(HISTORY_COL);
    final DBCollection to_coll = db.getCollection(BROKEN_LINKS_COL);
    final String prefix = ELEM_LST_FIELD + ".0.";
    final BasicDBObject query;

    if (fromDate == null) {
        query = new BasicDBObject("_id", docId);
    } else {/*from  w  w w  .  jav  a  2  s .com*/
        final SimpleDateFormat simple = new SimpleDateFormat(
                "yyyyMMdd" + (fromDate.contains(":") ? "-HH:mm:ss" : ""));
        final Date fDate = simple.parse(fromDate);
        query = new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", fDate));
    }
    final DBCursor cursor = from_coll.find(query);
    int total = 0;
    int reverted = 0;

    System.out.println("host=" + host);
    System.out.println("port=" + port);
    System.out.println("database=" + database);
    if (fromDate == null) {
        System.out.println("doc id=" + docId);
    } else {
        System.out.println("from date=" + fromDate);
    }
    System.out.println("found=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc_from = (BasicDBObject) cursor.next();
        final BasicDBObject doc_to = new BasicDBObject(doc_from);
        final String id = doc_from.getString(ID_FIELD);
        final BasicDBList list = (BasicDBList) doc_from.get(ELEM_LST_FIELD);
        final BasicDBObject elem = (BasicDBObject) list.get(0);
        final Date date = elem.getDate(LAST_UPDATE_FIELD);

        doc_to.put(LAST_UPDATE_FIELD, date);
        doc_to.put(BROKEN_URL_FIELD, elem.getString(BROKEN_URL_FIELD));
        doc_to.put(MSG_FIELD, elem.getString(MSG_FIELD));
        doc_to.put(CENTER_FIELD, elem.get(CENTER_FIELD));
        doc_to.removeField(ELEM_LST_FIELD);

        final WriteResult wr = to_coll.save(doc_to, WriteConcern.ACKNOWLEDGED);
        if (wr.getCachedLastError().ok()) {
            final WriteResult wr2 = from_coll.remove(doc_from, WriteConcern.ACKNOWLEDGED);
            if (wr2.getCachedLastError().ok()) {
                reverted++;
                System.out.println("+++id=" + id + " date=" + date);
            } else {
                System.err.println("Document[" + id + "] delete error.");
            }
        } else {
            System.err.println("Document[" + id + "] update error.");
        }
        total++;
    }
    cursor.close();

    System.out.println("total/undo: " + total + "/" + reverted);
}

From source file:br.bireme.tmp.AddPrettyField.java

License:Open Source License

private static void add(final String mongo_host, final int mongo_port, final String mongo_db,
        final String mongo_col) throws UnknownHostException {
    assert mongo_host != null;
    assert mongo_port > 0;
    assert mongo_db != null;
    assert mongo_col != null;

    final MongoClient client = new MongoClient(mongo_host, mongo_port);
    final DB db = client.getDB(mongo_db);
    final DBCollection coll = db.getCollection(HISTORY_COL);
    final DBCursor cursor = coll.find();
    int total = 0;

    System.out.println("host=" + mongo_host);
    System.out.println("port=" + mongo_port);
    System.out.println("database=" + mongo_db);
    System.out.println("collection=" + mongo_col);
    System.out.println("num of documents=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc1 = (BasicDBObject) cursor.next();
        final BasicDBList list = (BasicDBList) doc1.get(ELEM_LST_FIELD);

        for (Object obj : list) {
            final BasicDBObject doc2 = (BasicDBObject) obj;
            if (!doc2.containsField(PRETTY_BROKEN_URL_FIELD)) {
                final String burl = doc2.getString(BROKEN_URL_FIELD);
                try {
                    final String pburl = EncDecUrl.decodeUrl(burl);
                    doc2.append(PRETTY_BROKEN_URL_FIELD, pburl);

                    final WriteResult wr = coll.save(doc1, WriteConcern.ACKNOWLEDGED);
                    if (wr.getCachedLastError().ok()) {
                        total++;/*  w w w . j  a  va  2  s . c om*/
                    } else {
                        System.err.println("Document[" + doc1.getString("_id") + "] update error.");
                    }
                } catch (IOException ioe) {
                    System.err.println("Document[" + doc1.getString("_id") + "] bad encode conversion"
                            + " url=[" + burl + "]");
                }
            }
        }
    }
    cursor.close();
    System.out.println("num of added fields: " + total);
}

From source file:com.eharmony.matching.seeking.executor.mongodb.MongoQueryExecutor.java

License:Apache License

@VisibleForTesting
protected WriteResult save(DBCollection collection, DBObject entity) {
    return collection.save(entity, this.writeConcern);
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #save(java.lang.Object)/*from  w ww.  j a va 2 s  .c o m*/
 */
@Override
public <T> T save(T t) {
    T newT = null;
    if (t != null) {

        DBCollection dbcollection = getDBCollection(t.getClass(), true);

        String jsonObject;
        jsonObject = gson.toJson(t);
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(jsonObject.toString());
        dbObject.put("safe", "true");
        dbcollection.save(dbObject, WriteConcern.SAFE);
    }
    return newT;
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public void persistJoinTable(JoinTableData joinTableData) {
    String joinTableName = joinTableData.getJoinTableName();
    String joinColumnName = joinTableData.getJoinColumnName();
    String invJoinColumnName = joinTableData.getInverseJoinColumnName();
    Map<Object, Set<Object>> joinTableRecords = joinTableData.getJoinTableRecords();

    DBCollection dbCollection = mongoDb.getCollection(joinTableName);
    KunderaCoreUtils.printQuery("Persist join table:" + joinTableName, showQuery);

    for (Object key : joinTableRecords.keySet()) {
        Set<Object> values = joinTableRecords.get(key);
        Object joinColumnValue = key;

        for (Object childId : values) {
            DBObject dbObj = new BasicDBObject();
            dbObj.put("_id", joinColumnValue.toString() + childId);
            dbObj.put(joinColumnName, MongoDBUtils.populateValue(joinColumnValue, joinColumnValue.getClass()));
            dbObj.put(invJoinColumnName, MongoDBUtils.populateValue(childId, childId.getClass()));
            KunderaCoreUtils.printQuery("id:" + joinColumnValue.toString() + childId + "   " + joinColumnName
                    + ":" + joinColumnValue + "   " + invJoinColumnName + ":" + childId, showQuery);

            dbCollection.save(dbObj, getWriteConcern());
        }// ww w .j a  v a2  s . c o m
    }
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

/**
 * Executes on list of entities to be persisted.
 * // w  ww .  ja v  a  2  s .c  o  m
 * @param collections
 *            collection containing list of db objects.
 * @param entity
 *            entity in question.
 * @param id
 *            entity id.
 * @param metadata
 *            entity metadata
 * @param relationHolders
 *            relation holders.
 * @param isUpdate
 *            if it is an update
 * @return collection of DB objects.
 */
private Map<String, List<DBObject>> onPersist(Map<String, List<DBObject>> collections, Object entity, Object id,
        EntityMetadata metadata, List<RelationHolder> relationHolders, boolean isUpdate) {
    persistenceUnit = metadata.getPersistenceUnit();
    Map<String, DBObject> documents = handler.getDocumentFromEntity(metadata, entity, relationHolders,
            kunderaMetadata);

    if (isUpdate) {
        for (String documentName : documents.keySet()) {
            BasicDBObject query = new BasicDBObject();

            MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                    .getMetamodel(metadata.getPersistenceUnit());

            if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType())) {
                MongoDBUtils.populateCompoundKey(query, metadata, metaModel, id);
            } else {
                query.put("_id", MongoDBUtils.populateValue(id, id.getClass()));
            }
            DBCollection dbCollection = mongoDb.getCollection(documentName);
            KunderaCoreUtils.printQuery("Persist collection:" + documentName, showQuery);

            dbCollection.save(documents.get(documentName), getWriteConcern());
        }
    } else {
        for (String documentName : documents.keySet()) {
            // a db collection can have multiple records..
            // and we can have a collection of records as well.
            List<DBObject> dbStatements = null;
            if (collections.containsKey(documentName)) {
                dbStatements = collections.get(documentName);
                dbStatements.add(documents.get(documentName));
            } else {
                dbStatements = new ArrayList<DBObject>();
                dbStatements.add(documents.get(documentName));
                collections.put(documentName, dbStatements);
            }
        }
    }
    return collections;
}