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:net.joinedminds.masserr.db.impl.ManipulationDbImpl.java

License:Open Source License

@Override
public boolean deleteDiscipline(String id) {
    WriteResult result = db.get().delete(Discipline.class, new ObjectId(id));
    return result.getN() == 1;
}

From source file:net.skyebook.betaville.mongosession.MongoSessionTracker.java

License:Open Source License

@Override
public int killSession(String sessionToken) {
    DBObject query = new BasicDBObject(SessionConstants.SESSION_TOKEN, sessionToken);
    WriteResult results = collection.remove(query);
    if (results.getN() > 0)
        return 0;
    return -2;/*from   w  ww .  j  a va2  s  . c om*/
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java

License:Open Source License

/**
 * Updates a document in the database./*from   www. ja v a 2  s.  c  o  m*/
 */
public void update(DBCollection collection, DBObject query, DBObject document)
        throws UpdateException, StorageException {
    try {
        WriteResult writeResult = collection.update(query, document);
        if (writeResult.getN() == 0) {
            LOG.error("Failed to update {}", query);
            throw new UpdateException("Update failed");
        }
    } catch (MongoException e) {
        throw new StorageException(e);
    }
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java

License:Open Source License

/**
 * Removes documents from the database./* ww  w .j a  va 2 s. com*/
 */
public int remove(DBCollection collection, DBObject query) throws StorageException {
    try {
        WriteResult result = collection.remove(query);
        return (result != null) ? result.getN() : 0;
    } catch (MongoException e) {
        throw new StorageException(e);
    }
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoCommandStore.java

License:Open Source License

@Override
public boolean putClientCommand(ClientCommand command) {
    BasicDBObject query = new BasicDBObject();
    query.put("clientcommand.clientid", command.getClientId());
    DBObject object = MongoCommandMapper.clientCommandToDBObject(command);
    WriteResult update = commandCollection.update(query, object, true, false);
    return (update.getLastError().ok() && update.getN() == 1);
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoInfoStore.java

License:Open Source License

@Override
public boolean insertClientInfo(ClientInfo info, boolean listWorkingDir, boolean outputFiles) {
    // Changed to upsert
    BasicDBObject query = new BasicDBObject();
    query.put("clientinfo.clientid", info.getClientId());
    query.put("clientinfo.recipeid", info.getRecipeId());
    DBObject object = MongoInfoMapper.clientInfoToDBObject(info, listWorkingDir, outputFiles);
    WriteResult update = infoCollection.update(query, object, true, false);
    return (update.getLastError().ok() && update.getN() == 1);

    //DBObject bdo = MongoInfoMapper.clientInfoToDBObject(info, listWorkingDir, outputFiles);
    //WriteResult insert = infoCollection.insert(bdo);
    //return (insert.getLastError().ok() && insert.getN() == 1);
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public boolean putRecipe(Recipe r) {
    BasicDBObject bdo = new BasicDBObject();
    bdo.put("recipe", r.getIdentifier());
    WriteResult update = recipeCollection.update(bdo, MongoRecipeMapper.recipeToDBObject(r), true, false);
    return (update.getLastError().ok() && update.getN() == 1);
}

From source file:nl.vu.psy.rite.persistence.mongo.MongoRecipeStore.java

License:Open Source License

@Override
public boolean updateRecipe(Recipe oldRecipe, Recipe newRecipe) {
    // Compare and swap
    BasicDBObject bdo = new BasicDBObject();
    bdo.put("recipe", oldRecipe.getIdentifier());
    bdo.put("clientid", oldRecipe.getClientId());
    bdo.put("timestamp", TimeStamp.dateToString(oldRecipe.getTimeStamp()));
    WriteResult update = recipeCollection.update(bdo, MongoRecipeMapper.recipeToDBObject(newRecipe));
    return (update.getLastError().ok() && update.getN() == 1);
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Delete a folder entry based on the folder ID
 * /*www  . j  a  v  a2 s.c om*/
 * @param id                the folder ID
 */
public int deleteFolder(String context, String path, boolean cascade) {
    int deleted = 0;
    // Delete primary
    {
        DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path)
                .get();
        WriteResult wr = collection.remove(queryObj);
        deleted += wr.getN();
    }
    // Cascade
    if (cascade) {
        DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).push(FIELD_PATH)
                .add("$regex", "^" + path + "/").pop().get();
        WriteResult wr = collection.remove(queryObj);
        deleted += wr.getN();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Deleted folder: \n" + "   Context:        " + context + "\n" + "   Path:           "
                + path + "\n" + "   Cascade:        " + cascade + "\n" + "   Deleted:        " + deleted);
    }
    return deleted;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Increment the count of the subfolders in a folder.
 * /*from w  ww  .j a  v  a  2  s. c  o  m*/
 * @param context           the context in which the folder path is valid (mandatory)
 * @param path              the folder path relative to the given context
 * @param fileCountInc      the file count increment (can be negative)
 */
public void incrementFolderCount(String context, String path, long folderCountInc) {
    DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path).get();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$inc").add(FIELD_FOLDER_COUNT, folderCountInc).pop()
            .get();
    WriteResult result = collection.update(queryObj, updateObj);
    if (result.getN() != 1) {
        throw new RuntimeException("Failed to update folder's subfolder count: \n" + "   Context:  " + context
                + "\n" + "   Path:     " + path + "\n" + "   Result:   " + result);
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Incremented the subfolder count on " + context + "/" + path + " by " + folderCountInc);
    }
}