java - 如何使用spring数据在mongo中运行js文件

标签 java mongodb spring-data-mongodb

在 mongo shell 中可以使用 load 命令运行 js 文件:

load("path/to/file/file.js")

如何使用 spring-data 做到这一点?或Java中的任何其他方式。 我试过了:

BasicDBObject obj = new BasicDBObject();
obj.append( "$load" , "/path/file.js" );
CommandResult t=mongoTemplate.executeCommand(obj);

和:

obj.append( "$eval" , "load(\"/path/file.js\")" );

但它不起作用。

最佳答案

这里是 reference docs 的相关部分关于如何在 Spring Data MongoDB 中使用脚本。

ScriptOperations scriptOps = template.scriptOps();

// Execute script directly
ExecutableMongoScript echoScript = new ExecutableMongoScript("function(x) { return x; }");
scriptOps.execute(echoScript, "directly execute script");     

// Register script and call it later
scriptOps.register(new NamedMongoScript("echo", echoScript)); 
scriptOps.call("echo", "execute script via name");    

关于java - 如何使用spring数据在mongo中运行js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32486488/

相关文章:

java - 通过 SMTP 发送 ICS 文件无法在 Outlook 中正确显示?

node.js - 显示错误的最佳方式 Express、Jade 和 Mongo

Spring Data MongoDB,如何设置SSL?

MongoDB Atlas : user is not allowed to do action find on system. 索引

mongodb - 如何使用spring数据mongodb聚合在组中使用sum和condition

java - Android 使用单选组更改动态 View

java - 使用 Maven 进行 Vaadin 项目

mongodb 获取最后 30 分钟前插入的项目

java - Maven 项目上的 MyEclipse 2017 CI 10 错误

java - MongoDB Java 搜索数组大小