List of usage examples for org.springframework.data.mongodb.core.script ExecutableMongoScript ExecutableMongoScript
public ExecutableMongoScript(String code)
From source file:example.springdata.mongodb.advanced.ServersideScriptTests.java
/** * Store and call an arbitrary JavaScript function (in this case a simple echo script) via its name. *//* w w w . j ava 2 s . c om*/ @Test public void saveAndCallScriptViaName() { operations.scriptOps().register( new NamedMongoScript("echoScript", new ExecutableMongoScript("function(x) { return x; }"))); Object o = operations.scriptOps().call("echoScript", "Hello echo...!"); assertThat(o, is((Object) "Hello echo...!")); }
From source file:example.springdata.mongodb.advanced.ServersideScriptTests.java
private ExecutableMongoScript createExecutablePutIfAbsentScript(Customer customer) { String collectionName = operations.getCollectionName(Customer.class); Object id = operations.getConverter().getMappingContext().getPersistentEntity(Customer.class) .getIdentifierAccessor(customer).getIdentifier(); DBObject dbo = new BasicDBObject(); operations.getConverter().write(customer, dbo); String scriptString = String.format( "object = db.%1$s.findOne({\"_id\": \"%2$s\"}); if (object == null) { db.%1s.insert(%3$s); return null; } else { return object; }", collectionName, id, dbo);/*from w w w . ja v a 2s.c o m*/ return new ExecutableMongoScript(scriptString); }