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());
}
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());
}
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));
}
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);
}
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;
}
Aggregations