Package com.mongodb

Examples of com.mongodb.WriteResult


    @Test
    public void testNoWriteConcern() throws Exception {
        assertEquals(0, testCollection.count());
        Object result = template.requestBody("direct:noWriteConcern", "{\"scientist\":\"newton\"}");
        assertTrue("Result is not of type WriteResult", result instanceof WriteResult);
        WriteResult wr = (WriteResult) result;
        WriteConcern wc = wr.getLastConcern();
        // check the WriteConcern's behaviour
        if (wc.callGetLastError()) {
            assertNotNull(wr.getCachedLastError());
        } else {
            assertNull(wr.getCachedLastError());
        }
        CommandResult cr = wr.getLastError();
        assertTrue(cr.ok());
    }
View Full Code Here


        assertEquals(0, testCollection.count());
       
        // test with object first
        Object result = template.requestBodyAndHeader("direct:noWriteConcern", "{\"scientist\":\"newton\"}", MongoDbConstants.WRITECONCERN, WriteConcern.SAFE);
        assertTrue("Result is not of type WriteResult", result instanceof WriteResult);
        WriteResult wr = (WriteResult) result;
        // should not be null because with WriteConcern.SAFE, getLastError was called implicitly by the driver
        assertNotNull(wr.getCachedLastError());
        CommandResult cr = wr.getLastError();
        assertTrue(cr.ok());
       
        // same behaviour should be reproduced with String 'SAFE'
        result = template.requestBodyAndHeader("direct:noWriteConcern", "{\"scientist\":\"newton\"}", MongoDbConstants.WRITECONCERN, "SAFE");
        assertTrue("Result is not of type WriteResult", result instanceof WriteResult);
        wr = (WriteResult) result;
        // should not be null because with WriteConcern.SAFE, getLastError was called implicitly by the driver
        assertNotNull(wr.getCachedLastError());
        cr = wr.getLastError();
        assertTrue(cr.ok());
    }
View Full Code Here

    protected void doRemove(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        DBObject removeObj = exchange.getIn().getMandatoryBody(DBObject.class);
       
        WriteConcern wc = extractWriteConcern(exchange);
        WriteResult result = wc == null ? dbCol.remove(removeObj) : dbCol.remove(removeObj, wc);
       
        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.remove);
        // we always return the WriteResult, because whether the getLastError was called or not,
        // the user will have the means to call it or obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
        resultMessage.setHeader(MongoDbConstants.RECORDS_AFFECTED, result.getN());
    }
View Full Code Here

        DBObject objNew = saveObj.get(1);
       
        Boolean multi = exchange.getIn().getHeader(MongoDbConstants.MULTIUPDATE, Boolean.class);
        Boolean upsert = exchange.getIn().getHeader(MongoDbConstants.UPSERT, Boolean.class);
       
        WriteResult result;
        WriteConcern wc = extractWriteConcern(exchange);
        // In API 2.7, the default upsert and multi values of update(DBObject, DBObject) are false, false, so we unconditionally invoke the
        // full-signature method update(DBObject, DBObject, boolean, boolean). However, the default behaviour may change in the future,
        // so it's safer to be explicit at this level for full determinism
        if (multi == null && upsert == null) {
            // for update with no multi nor upsert but with specific WriteConcern there is no update signature without multi and upsert args,
            // so assume defaults
            result = wc == null ? dbCol.update(updateCriteria, objNew) : dbCol.update(updateCriteria, objNew, false, false, wc);
        } else {
            // we calculate the final boolean values so that if any of these parameters is null, it is resolved to false
            result = wc == null ? dbCol.update(updateCriteria, objNew, calculateBooleanValue(upsert), calculateBooleanValue(multi))
                    : dbCol.update(updateCriteria, objNew, calculateBooleanValue(upsert), calculateBooleanValue(multi), wc);
        }
       
        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.update);
        // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or
        // obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
        resultMessage.setHeader(MongoDbConstants.RECORDS_AFFECTED, result.getN());
    }
View Full Code Here

    protected void doSave(Exchange exchange) throws Exception {
        DBCollection dbCol = calculateCollection(exchange);
        DBObject saveObj = exchange.getIn().getMandatoryBody(DBObject.class);
       
        WriteConcern wc = extractWriteConcern(exchange);
        WriteResult result = wc == null ? dbCol.save(saveObj) : dbCol.save(saveObj, wc);
       
        prepareResponseMessage(exchange, MongoDbOperation.save);
        // we always return the WriteResult, because whether the getLastError was called or not, the user will have the means to call it or
        // obtain the cached CommandResult
        processAndTransferWriteResult(result, exchange);
View Full Code Here

            } else {
                throw new CamelMongoDbException("MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>");
            }
        }
       
        WriteResult result;
        WriteConcern wc = extractWriteConcern(exchange);
        if (singleInsert) {
            result = wc == null ? dbCol.insert((DBObject) insert) : dbCol.insert((DBObject) insert, wc);
        } else {
            result = wc == null ? dbCol.insert((List<DBObject>) insert) : dbCol.insert((List<DBObject>) insert, wc);
View Full Code Here

        return hydrate(mongoTemplate.find(query, dbType, collection));
    }

    @Override
    public int update(Query query, Update update) {
        WriteResult result = mongoTemplate.updateMulti(query, update, collection);
        return result.getN();
    }
View Full Code Here

        BasicDBObject comment = new BasicDBObject("author", name).append("body", body);
        if (email != null && !email.equals("")) {
            comment.append("email", email);
        }

        WriteResult result = postsCollection.update(new BasicDBObject("permalink", permalink),
                new BasicDBObject("$push", new BasicDBObject("comments", comment)), false, false);
    }
View Full Code Here

        BasicDBObject comment = new BasicDBObject("author", name).append("body", body);
        if (email != null && !email.equals("")) {
            comment.append("email", email);
        }

        WriteResult result = postsCollection.update(new BasicDBObject("permalink", permalink),
                new BasicDBObject("$push", new BasicDBObject("comments", comment)), false, false);
    }
View Full Code Here

        lockDoc.put(LockDef.OWNER_THREAD_GROUP_NAME.field, currentThread.getThreadGroup().getName());
        lockDoc.put(LockDef.LOCK_ATTEMPT_COUNT.field, 0);
        lockDoc.put(LockDef.INACTIVE_LOCK_TIMEOUT.field, pLockOptions.getInactiveLockTimeout());

        // Insert, if successful then get out of here.
        final WriteResult result = getDbCollection(pMongo, pSvcOptions).insert(lockDoc, WriteConcern.NORMAL);
        final CommandResult cmdResult = result.getLastError();

        if (cmdResult.getErrorMessage() != null) return null;

       if (pSvcOptions.getEnableHistory())
       { LockHistoryDao.insert( pMongo, pLockName, pSvcOptions, pLockOptions, serverTime, LockState.LOCKED, lockId, false); }
View Full Code Here

TOP

Related Classes of com.mongodb.WriteResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.