@Override public Object call(final String scriptName, final Object... args) { Assert.hasText(scriptName, "ScriptName must not be null or empty!"); return mongoOperations.execute(new DbCallback<Object>() { @Override public Object doInDB(MongoDatabase db) throws MongoException, DataAccessException { return db.runCommand(new Document("eval", String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(args)))) .get("retval"); } }); }
@Override public Object execute(final ExecutableMongoScript script, final Object... args) { Assert.notNull(script, "Script must not be null!"); return mongoOperations.execute(new DbCallback<Object>() { @Override public Object doInDB(MongoDatabase db) throws MongoException, DataAccessException { Document command = new Document("$eval", script.getCode()); BasicDBList commandArgs = new BasicDBList(); commandArgs.addAll(Arrays.asList(convertScriptArgs(false, args))); command.append("args", commandArgs); return db.runCommand(command).get("retval"); } }); }
@Override public com.mongodb.bulk.BulkWriteResult execute() { try { return mongoOperations.execute(collectionName, collection -> { return collection.bulkWrite(models.stream().map(this::mapWriteModel).collect(Collectors.toList()), bulkOptions); }); } finally { this.bulkOptions = getBulkWriteOptions(bulkOperationContext.getBulkMode()); } }
@Override public Object call(final String scriptName, final Object... args) { Assert.hasText(scriptName, "ScriptName must not be null or empty!"); return mongoOperations.execute(new DbCallback<Object>() { @Override public Object doInDB(MongoDatabase db) throws MongoException, DataAccessException { return db.runCommand(new Document("eval", String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(args)))) .get("retval"); } }); }
@Override public Object execute(final ExecutableMongoScript script, final Object... args) { Assert.notNull(script, "Script must not be null!"); return mongoOperations.execute(new DbCallback<Object>() { @Override public Object doInDB(MongoDatabase db) throws MongoException, DataAccessException { Document command = new Document("$eval", script.getCode()); BasicDBList commandArgs = new BasicDBList(); commandArgs.addAll(Arrays.asList(convertScriptArgs(false, args))); command.append("args", commandArgs); return db.runCommand(command).get("retval"); } }); }
@Override public com.mongodb.bulk.BulkWriteResult execute() { try { return mongoOperations.execute(collectionName, collection -> { return collection.bulkWrite(models.stream().map(this::mapWriteModel).collect(Collectors.toList()), bulkOptions); }); } finally { this.bulkOptions = getBulkWriteOptions(bulkOperationContext.getBulkMode()); } }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { String collectionName = this.collectionNameExpression.getValue(this.evaluationContext, requestMessage, String.class); // TODO: 5.2 assert not null Object result; if (this.collectionCallback != null) { result = this.mongoTemplate.execute(collectionName, // NOSONAR collection -> this.collectionCallback.doInCollection(collection, requestMessage)); } else { Query query = buildQuery(requestMessage); if (this.expectSingleResult) { result = this.mongoTemplate.findOne(query, this.entityClass, collectionName); // NOSONAR } else { result = this.mongoTemplate.find(query, this.entityClass, collectionName); // NOSONAR } } return result; }
class MyClient { private final MongoOperations operations; @Autowired public MyClient(MongoOperations mongoOperations) { this.operations = operations; } void yourMethod() { operations.execute(new CollectionCallback<YourDomainClass>() { YourDomainClass doInCollection(DBCollection collection) { // here goes your low-level code } }); }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { String collectionName = this.collectionNameExpression.getValue(this.evaluationContext, requestMessage, String.class); // TODO: 5.2 assert not null Object result; if (this.collectionCallback != null) { result = this.mongoTemplate.execute(collectionName, // NOSONAR collection -> this.collectionCallback.doInCollection(collection, requestMessage)); } else { Query query = buildQuery(requestMessage); if (this.expectSingleResult) { result = this.mongoTemplate.findOne(query, this.entityClass, collectionName); // NOSONAR } else { result = this.mongoTemplate.find(query, this.entityClass, collectionName); // NOSONAR } } return result; }