Optic API:如何获取具有最大值的列的值?
Optic API: how do I get the value of a column that goes with a max value?
我的数据集有两个数组,其中包含具有两个属性(日期和值)的对象。对于每个数组,我需要获取具有最新日期的对象。我正在尝试从索引中执行此操作,并正在探索使用 Optic API 进行查询。
我的视图有三列:“statusType”,表示值来自哪个数组; “状态日期”;和“价值”。通过以下查询,我可以获取每种类型的最新日期,但我不知道如何获取相应的值。
const op = require('/MarkLogic/optic');
op.fromView('Parent', 'info')
.where(cts.documentQuery('/test/doc1.json'))
.groupBy([op.col('statusType')], [op.max('maxdate', op.col('statusDate'))])
.result()
产生:
{
"statusType": "subtype1",
"maxdate": "2020-09-29T16:33:18.6301434-04:00"
},
{
"statusType": "subtype2",
"maxdate": "2020-08-29T16:33:18.6301434-04:00"
}
如果我将 value
添加到 groupBy
的第一个参数,我将获得类型和值的所有不同组合(使用 maxdate)。如果我将 value
添加到 groupBy
的第二个参数,我得到的是最后一个值,而不是与 maxdate
.
关联的值
预期输出:
{
"statusType": "subtype1",
"value": "valueB",
"maxdate": "2020-09-29T16:33:18.6301434-04:00"
},
{
"statusType": "subtype2",
"value": "valueC",
"maxdate": "2020-08-29T16:33:18.6301434-04:00"
}
示例数据:
'use strict';
declareUpdate();
xdmp.documentInsert(
'/test/doc1.json',
{
"parent": {
"subtype1": [
{
"value": "valueA",
"date": "2020-07-29T16:33:18.6301434-04:00"
},
{
"value": "valueB",
"date": "2020-09-29T16:33:18.6301434-04:00"
}
],
"subtype2": [
{
"value": "valueC",
"date": "2020-08-29T16:33:18.6301434-04:00"
},
{
"value": "valueD",
"date": "2020-07-29T16:33:18.6301434-04:00"
}
]
}
}
)
模板 1:
declareUpdate();
const tde = require("/MarkLogic/tde.xqy");
let template =
xdmp.toJSON(
{
"template": {
"context": "/parent/subtype1",
"rows": [
{
"schemaName": "Parent",
"viewName": "info",
"columns": [
{
"name": "statusType",
"scalarType": "string",
"val": "'subtype1'"
},
{
"name": "value",
"scalarType": "string",
"val": "value"
},
{
"name": "statusDate",
"scalarType": "dateTime",
"val": "date"
}
]
}
]
}
}
);
// comment and uncomment based on which action you want to take
let action =
//'validate'
//'extract'
'insert'
;
if (action === 'validate') {
tde.validate([template]);
} else if (action === 'extract') {
tde.nodeDataExtract([cts.doc( "/test/doc1.json" )], [template])
} else if (action === 'insert') {
tde.templateInsert("/tde/subtype1.json", template, xdmp.defaultPermissions(), ["TDE"])
}
模板 2:
declareUpdate();
const tde = require("/MarkLogic/tde.xqy");
let template =
xdmp.toJSON(
{
"template": {
"context": "/parent/subtype2",
"rows": [
{
"schemaName": "Parent",
"viewName": "info",
"columns": [
{
"name": "statusType",
"scalarType": "string",
"val": "'subtype2'"
},
{
"name": "value",
"scalarType": "string",
"val": "value"
},
{
"name": "statusDate",
"scalarType": "dateTime",
"val": "date"
}
]
}
]
}
}
);
// comment and uncomment based on which action you want to take
let action =
//'validate'
//'extract'
'insert'
;
if (action === 'validate') {
tde.validate([template]);
} else if (action === 'extract') {
tde.nodeDataExtract([cts.doc( "/test/doc1.json" )], [template])
} else if (action === 'insert') {
tde.templateInsert("/tde/subtype2.json", template, xdmp.defaultPermissions(), ["TDE"])
}
致敬,卡塞尔先生:
如果我正确理解了要求,我知道的唯一方法是将 groupBy()
结果与原始视图结合起来:
groupBy()
发出带有分组键和 max() 聚合值的行,在 fromView()
访问器上传递别名/限定符名称。
- 通过连接同一视图获取最大行的其余列(使用 maxInfo.statusType=info.statusType 和 maxInfo.maxdate=info.statusDate 上的连接键) .
groupBy()
操作对聚合参数中的任何列进行采样。
希望对您有所帮助,
我的数据集有两个数组,其中包含具有两个属性(日期和值)的对象。对于每个数组,我需要获取具有最新日期的对象。我正在尝试从索引中执行此操作,并正在探索使用 Optic API 进行查询。
我的视图有三列:“statusType”,表示值来自哪个数组; “状态日期”;和“价值”。通过以下查询,我可以获取每种类型的最新日期,但我不知道如何获取相应的值。
const op = require('/MarkLogic/optic');
op.fromView('Parent', 'info')
.where(cts.documentQuery('/test/doc1.json'))
.groupBy([op.col('statusType')], [op.max('maxdate', op.col('statusDate'))])
.result()
产生:
{
"statusType": "subtype1",
"maxdate": "2020-09-29T16:33:18.6301434-04:00"
},
{
"statusType": "subtype2",
"maxdate": "2020-08-29T16:33:18.6301434-04:00"
}
如果我将 value
添加到 groupBy
的第一个参数,我将获得类型和值的所有不同组合(使用 maxdate)。如果我将 value
添加到 groupBy
的第二个参数,我得到的是最后一个值,而不是与 maxdate
.
预期输出:
{
"statusType": "subtype1",
"value": "valueB",
"maxdate": "2020-09-29T16:33:18.6301434-04:00"
},
{
"statusType": "subtype2",
"value": "valueC",
"maxdate": "2020-08-29T16:33:18.6301434-04:00"
}
示例数据:
'use strict';
declareUpdate();
xdmp.documentInsert(
'/test/doc1.json',
{
"parent": {
"subtype1": [
{
"value": "valueA",
"date": "2020-07-29T16:33:18.6301434-04:00"
},
{
"value": "valueB",
"date": "2020-09-29T16:33:18.6301434-04:00"
}
],
"subtype2": [
{
"value": "valueC",
"date": "2020-08-29T16:33:18.6301434-04:00"
},
{
"value": "valueD",
"date": "2020-07-29T16:33:18.6301434-04:00"
}
]
}
}
)
模板 1:
declareUpdate();
const tde = require("/MarkLogic/tde.xqy");
let template =
xdmp.toJSON(
{
"template": {
"context": "/parent/subtype1",
"rows": [
{
"schemaName": "Parent",
"viewName": "info",
"columns": [
{
"name": "statusType",
"scalarType": "string",
"val": "'subtype1'"
},
{
"name": "value",
"scalarType": "string",
"val": "value"
},
{
"name": "statusDate",
"scalarType": "dateTime",
"val": "date"
}
]
}
]
}
}
);
// comment and uncomment based on which action you want to take
let action =
//'validate'
//'extract'
'insert'
;
if (action === 'validate') {
tde.validate([template]);
} else if (action === 'extract') {
tde.nodeDataExtract([cts.doc( "/test/doc1.json" )], [template])
} else if (action === 'insert') {
tde.templateInsert("/tde/subtype1.json", template, xdmp.defaultPermissions(), ["TDE"])
}
模板 2:
declareUpdate();
const tde = require("/MarkLogic/tde.xqy");
let template =
xdmp.toJSON(
{
"template": {
"context": "/parent/subtype2",
"rows": [
{
"schemaName": "Parent",
"viewName": "info",
"columns": [
{
"name": "statusType",
"scalarType": "string",
"val": "'subtype2'"
},
{
"name": "value",
"scalarType": "string",
"val": "value"
},
{
"name": "statusDate",
"scalarType": "dateTime",
"val": "date"
}
]
}
]
}
}
);
// comment and uncomment based on which action you want to take
let action =
//'validate'
//'extract'
'insert'
;
if (action === 'validate') {
tde.validate([template]);
} else if (action === 'extract') {
tde.nodeDataExtract([cts.doc( "/test/doc1.json" )], [template])
} else if (action === 'insert') {
tde.templateInsert("/tde/subtype2.json", template, xdmp.defaultPermissions(), ["TDE"])
}
致敬,卡塞尔先生:
如果我正确理解了要求,我知道的唯一方法是将 groupBy()
结果与原始视图结合起来:
groupBy()
发出带有分组键和 max() 聚合值的行,在fromView()
访问器上传递别名/限定符名称。- 通过连接同一视图获取最大行的其余列(使用 maxInfo.statusType=info.statusType 和 maxInfo.maxdate=info.statusDate 上的连接键) .
groupBy()
操作对聚合参数中的任何列进行采样。
希望对您有所帮助,