如果你需要他们回来作为一个JSON数组,你正在做正确的事情。我知道没有更优雅/更快的解决方案。但是,如果您正在寻找更优化/高性能的方式,请尝试使用REST extensions,这会将文档序列转换为多部分HTTP响应。
下面是一个例子。鉴于example.sjs与内容:
function get(context, params) {
return fn.collection('my-users')
}
exports.GET = get;
安装像这样:
curl --anyauth --user admin:admin -X PUT -i \
-H "Content-type: application/vnd.marklogic-javascript" \
--data-binary @./example.sjs \
http://localhost:8000/LATEST/config/resources/js-example
而下面的文档插入我的用户集合(我假设你知道如何将这些):
myuser.json
{"name":"Sue"}
myuser2.json
{"name":"Mary"}
myuser3.json
{"name":"Jane"}
myuser4.json
{"name":"Joe"}
你可以叫你休息的扩展,像这样:
curl --anyauth --user admin:admin \
http://localhost:8000/LATEST/resources/js-example
,你会得到下面的多部分http回应:
--js-example-get-result
Content-Type: application/json
Content-Length: 14
{"name":"Sue"}
--js-example-get-result
Content-Type: application/json
Content-Length: 15
{"name":"Mary"}
--js-example-get-result
Content-Type: application/json
Content-Length: 15
{"name":"Jane"}
--js-example-get-result
Content-Type: application/json
Content-Length: 14
{"name":"Joe"}
--js-example-get-result--
使用您最喜爱的客户端http库来接受作为每个文档的单个json文档的高效响应。
我应该补充说,如果您的要求很简单,那么不需要REST扩展。您可以简单地使用REST搜索端点:
curl --anyauth --user admin:admin \
-H accept:multipart/mixed \
http://localhost:8000/LATEST/search?collection=my-users
并获得非常相似的多部分http响应。我只提供了REST扩展的例子,因为你的问题是关于服务器端的JavaScript,我想你可能会有额外的需求。
并不清楚你所要求的东西。输出应该是什么样子?你想为每个文件提供JSON表示吗?文件是JSON还是XML?另请参阅http://docs.marklogic.com并选择左上窗格中的“服务器端JavaScript API”部分。大多数XQuery函数也可用于服务器端JavaScript。 – scotthenninger
fn.collection('my-users')。toArray()就是我需要的 –