Example usage for com.mongodb WriteResult getN

List of usage examples for com.mongodb WriteResult getN

Introduction

In this page you can find the example usage for com.mongodb WriteResult getN.

Prototype

public int getN() 

Source Link

Document

Gets the "n" field, which contains the number of documents affected in the write operation.

Usage

From source file:org.wisdom.jongo.bridge.JongoCRUDService.java

License:Apache License

/**
 * Delete an object by  from the collection if it exists.
 *
 * @param id of the object you wish to delete for.
 *           If the id doesn't exist there is an IllegalArgumentException.
 *           <p>/*  ww  w. j a v a 2 s .c  o  m*/
 *           Note: as far as I can tell jongo only supports remove for object id types and not others.
 */
@Override
public void delete(K id) {
    WriteResult result;
    if (idFieldType.equals(ObjectId.class)) {
        result = collection.remove(withOid(String.valueOf(id)));
    } else {
        result = collection.remove(createIdQuery(id));

    }
    //get n is number of docs effected by operation in mongo
    if (result.getN() == 0) {
        throw new IllegalArgumentException("Unable to delete Id '" + id + "' not found");
    }
}

From source file:pl.nask.hsn2.os.MongoConnector.java

License:Open Source License

public void removeByJobId(long jobId) {
    long start = System.currentTimeMillis();
    DBObject ref = new BasicDBObject();
    ref.put("job_id", jobId);
    int counter = 0;

    WriteResult res;
    while ((res = collection.remove(ref)).getN() != 0) {
        counter += res.getN();
    }/* w w w .  j  a  va2  s .  c om*/
    long stop = System.currentTimeMillis();
    long time = (stop - start) / 1000;
    LOGGER.debug("Objects removed: {} in {} s", counter, time);
}

From source file:rapture.sheet.mongodb.MongoMetaSheetStore.java

License:Open Source License

private Boolean standardDelete(String name, String itemName, String type) {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.append(TYPE, type);/*from  w  w  w.j a v a 2  s  .  c om*/
    query.append(KEY, name);
    query.append(NAME, itemName);
    WriteResult res = collection.remove(query);
    return res.getN() != 0;
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageLogService.java

License:Open Source License

@Override
public int archiveMessages(long messageAgeInSeconds) {

    //check the timestamp for old messages
    Date dateTime = new Date(System.currentTimeMillis() - messageAgeInSeconds * 1000);

    //criteria for automatic archiving
    Query query = new Query();
    query.addCriteria(Criteria.where("arrivalTimeStamp").lt(dateTime).and("archived").in(false, null)
            .orOperator(Criteria.where("state").in("NEW", "SENT", "FETCHED", "QUARANTINED"),
                    Criteria.where("label.transferType").is("SYNCH")));

    //update the archived flag and stateTimestamp value
    Update update = new Update();

    update.set("stateTimeStamp", new Date());
    update.set("archived", true);

    //update all matches in the mongodb
    WriteResult wr = mongoTemplate.updateMulti(query, update, ShsMessageEntry.class);

    log.debug("Archived {} messages modified before {}", wr.getN(), dateTime);
    return wr.getN();
}

From source file:testt.mongoConnector.java

public boolean pushToArray(String key, Object val, String field, Object o) {
    WriteResult res;
    try {/*from w  ww  .jav a  2  s  .c  o  m*/
        BasicDBObject query = new BasicDBObject(key, val);
        BasicDBObject push = new BasicDBObject("$push", new BasicDBObject(field, o));
        res = coll.update(query, push, false, true, wc);
    } catch (Exception e) {
        return false;
    }
    return res.getN() > 0;
}

From source file:uk.gov.nationalarchives.discovery.taxonomy.common.repository.mongo.impl.TrainingDocumentRepositoryImpl.java

License:Mozilla Public License

@Override
public int deleteByCategory(String categoryName) {
    Query query = new Query(Criteria.where("CATEGORY").is(categoryName));
    WriteResult writeResult = categoriesMongoTemplate.remove(query, TrainingDocument.class);
    return writeResult.getN();
}

From source file:v7cr.v7db.Versioning.java

License:Open Source License

/**
 * updates an object, but only if the base version has not been changed in
 * the meantime. The object must have an _id field. The _version field if
 * present is ignored, and set to baseVersion+1.
 *///from   w  w  w .  j  av  a 2  s  .c  o  m

public static WriteResult update(DBCollection collection, final int baseVersion, DBObject object) {
    object.put(VERSION, baseVersion + 1);
    Object id = object.get("_id");
    WriteResult result = collection.update(new BasicDBObject("_id", id).append(VERSION, baseVersion), object);
    if (result.getN() != 1)
        throw new ConcurrentModificationException("baseVersion has changed");
    return result;
}

From source file:v7db.files.mongodb.MongoReferenceTracking.java

License:Open Source License

public void updateReferences(Object ownerId, ContentPointer... contents) throws IOException {
    List<byte[]> content = new ArrayList<byte[]>();
    for (ContentPointer cp : contents) {
        if (cp instanceof InlineContent)
            continue;
        if (cp instanceof StoredContent)
            content.add(((StoredContent) cp).getBaseSHA());
        else if (cp instanceof ContentSHA)
            content.add(((ContentSHA) cp).getSHA());
        else/*from   w  w  w .ja v  a  2  s .com*/
            throw new IllegalArgumentException(cp.getClass().getName());
    }

    WriteResult r = refCollection.update(new BasicDBObject("_id", ownerId),
            new BasicDBObject("$set", new BasicDBObject("refs", content)).append("$addToSet",
                    new BasicDBObject("refHistory", new BasicDBObject("$each", content))),
            false, false, WriteConcern.SAFE);
    if (r.getN() == 1)
        return;
    if (r.getN() != 0)
        throw new IllegalStateException();
    refCollection.insert(WriteConcern.SAFE,
            new BasicDBObject("_id", ownerId).append("refs", content).append("refHistory", content));

}