You need to enable JavaScript to run this app. MarkLogic Node.js 查询如何指定返回属性以减少响应数据量
优惠活动
大模型
产品
解决方案
定价
更多

MarkLogic Node.js 查询如何指定返回属性以减少响应数据量

MarkLogic Node.js 查询指定返回字段解决方案

要实现仅返回目标字段、减少响应数据体量,可以通过select方法配合extract选项实现服务端字段过滤,以下是适配你场景的修改后代码:

var ml = require('marklogic');
var conn = require('./config.js').connection;
var db = ml.createDatabaseClient(conn);
var qb = ml.queryBuilder;

db.documents.query(
  qb.where(
    qb.collection('test')   
  )
  // 指定需要返回的属性
  .select(qb.property('summary'))
  .withOptions({
    metrics: true,
    // 开启服务端字段提取,明确指定属性路径
    extract: {paths: ['summary']}
  })
).result()
.then(function(docs) {
  console.log('This search found: ' + JSON.stringify(docs[1]));
})
.catch(function(error) {
  console.log('something went wrong: ' + error);
});

配置说明

  • 服务端会直接在查询阶段过滤字段,不会返回完整文档,大幅降低传输的数据量
  • 如需返回多个字段,可按如下方式调整配置:
    .select(qb.property('summary'), qb.property('foo'))
    .withOptions({
      metrics: true,
      extract: {paths: ['summary', 'foo']}
    })
    
  • 针对嵌套层级的属性,直接填写完整JSON路径即可,比如提取other数组下的key字段,路径填写为other.key

内容的提问来源于stack exchange,提问作者dalvir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 09:39:02