Search in sources :

Example 21 with WriteResult

use of com.mongodb.WriteResult in project camel by apache.

the class MongoDbWriteConcernsTest method testDynamicWriteConcernSafe.

@Test
@Ignore
public void testDynamicWriteConcernSafe() throws Exception {
    assertEquals(0, testCollection.count());
    // test with object first
    Object result = template.requestBody("direct:noWriteConcern", "{\"scientist\":\"newton\"}");
    assertTrue("Result is not of type WriteResult", result instanceof WriteResult);
    WriteResult wr = (WriteResult) result;
    assertTrue(wr.wasAcknowledged());
    // same behaviour should be reproduced with String 'SAFE'
    result = template.requestBody("direct:noWriteConcern", "{\"scientist\":\"newton\"}");
    assertTrue("Result is not of type WriteResult", result instanceof WriteResult);
    wr = (WriteResult) result;
    assertTrue(wr.wasAcknowledged());
}
Also used : WriteResult(com.mongodb.WriteResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with WriteResult

use of com.mongodb.WriteResult in project spring-data-document-examples by spring-projects.

the class SimpleMongoTest method updatingDocuments.

@Test
public void updatingDocuments() {
    String collectionName = operations.getCollectionName(Person.class);
    Person p1 = new Person("Bob", 33);
    p1.addAccount(new Account("198-998-2188", Account.Type.SAVINGS, 123.55d));
    operations.insert(p1);
    Person p2 = new Person("Mary", 25);
    p2.addAccount(new Account("860-98107-681", Account.Type.CHECKING, 400.51d));
    operations.insert(p2);
    Person p3 = new Person("Chris", 68);
    p3.addAccount(new Account("761-002-8901", Account.Type.SAVINGS, 10531.00d));
    operations.insert(p3);
    Person p4 = new Person("Janet", 33);
    p4.addAccount(new Account("719-100-0019", Account.Type.SAVINGS, 1209.10d));
    operations.insert(p4);
    assertEquals(4, operations.getCollection(collectionName).count());
    WriteResult wr = operations.updateMulti(query(where("accounts.accountType").is(Account.Type.SAVINGS.name())), new Update().inc("accounts.$.balance", 50.00), Person.class);
    assertNotNull(wr);
    assertEquals(3, wr.getN());
}
Also used : Account(org.springframework.data.mongodb.examples.hello.domain.Account) WriteResult(com.mongodb.WriteResult) Update(org.springframework.data.mongodb.core.query.Update) Person(org.springframework.data.mongodb.examples.hello.domain.Person) Test(org.junit.Test)

Example 23 with WriteResult

use of com.mongodb.WriteResult in project v7files by thiloplanz.

the class MongoReferenceTracking method updateReferences.

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
            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));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) ContentSHA(v7db.files.spi.ContentSHA) ContentPointer(v7db.files.spi.ContentPointer) ArrayList(java.util.ArrayList) InlineContent(v7db.files.spi.InlineContent) StoredContent(v7db.files.spi.StoredContent)

Example 24 with WriteResult

use of com.mongodb.WriteResult in project v7files by thiloplanz.

the class V7GridFS method insertMetaData.

private void insertMetaData(DBObject metaData) throws IOException {
    metaData.put("_version", 1);
    metaData.put("created_at", new Date());
    WriteResult result = files.insert(WriteConcern.SAFE, metaData);
    String error = result.getError();
    if (error != null)
        throw new IOException(error);
}
Also used : WriteResult(com.mongodb.WriteResult) IOException(java.io.IOException) Date(java.util.Date)

Example 25 with WriteResult

use of com.mongodb.WriteResult in project v7files by thiloplanz.

the class Vermongo method remove.

/**
	 * deletes the object without checking for conflicts. An existing version is
	 * moved to the shadow collection, along with a dummy version to mark the
	 * deletion. This dummy version can contain optional meta-data (such as who
	 * deleted the object, and when).
	 */
static DBObject remove(DBCollection collection, Object id, BSONObject metaData) {
    DBObject base = collection.findOne(new BasicDBObject("_id", id));
    if (base == null)
        return null;
    // copy to shadow
    DBCollection shadow = getShadowCollection(collection);
    int version = getVersion(base);
    BasicDBObject revId = new BasicDBObject("_id", getId(base)).append(_VERSION, version);
    base.put("_id", revId);
    WriteResult r = shadow.insert(base, WriteConcern.SAFE);
    // TODO: if already there, no error
    r.getLastError().throwOnError();
    // add the dummy version
    BasicDBObject dummy = new BasicDBObject("_id", revId.append(_VERSION, version + 1)).append(_VERSION, "deleted:" + (version + 1));
    if (metaData != null)
        dummy.putAll(metaData);
    r = shadow.insert(dummy, WriteConcern.SAFE);
    // TODO: if already there, no error
    r.getLastError().throwOnError();
    collection.remove(new BasicDBObject("_id", id));
    return base;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

WriteResult (com.mongodb.WriteResult)27 BasicDBObject (com.mongodb.BasicDBObject)17 DBObject (com.mongodb.DBObject)14 DBCollection (com.mongodb.DBCollection)9 Test (org.junit.Test)5 BulkWriteResult (com.mongodb.BulkWriteResult)3 DB (com.mongodb.DB)3 Date (java.util.Date)3 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2 MongoException (com.mongodb.MongoException)2 QueryBuilder (com.mongodb.QueryBuilder)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 ObjectId (org.bson.types.ObjectId)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)2 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)2 Stream (org.graylog2.plugin.streams.Stream)2