简体   繁体   English

使用Marklogic Optic API格式化日期

[英]Date Formatting with Marklogic Optic API

My question is about getting the MarkLogic query console javascript API to format a column of strings to dates. 我的问题是关于让MarkLogic查询控制台javascript API将一列字符串格式化为日期。

Working on a string directly works as expected: 直接处理字符串可以按预期工作:

var d = new Date("3/12/2019");
xdmp.monthNameFromDate(xs.date(d))
>>> March

Working with the optic api however: 但是,使用光学API:

const op = require('/MarkLogic/optic');

const ind = op.fromView('schema', 'money');
//get non null dates, stored as strings, [MM-DD-YYYY]
const ind2 = ind.where(op.ne(op.col('completed date'), ""))
const testMonth = op.as('testDate', fn.formatDate(xs.date(op.col('completed date')), "[M01]-[D01]-[Y0001]"))

Returns the following error: 返回以下错误:

[javascript] XDMP-CAST: function bound ()() -- Invalid cast: {_expr:"¿\"completed date\"", _preplans:null, _schemaName:null, ...} cast as xs.date

I believe this is different than the other questions on this topic because those didn't involve the OPTIC API as far as I can tell, and were resolved by just operating on single strings. 我相信这与该主题上的其他问题有所不同,因为据我所知,这些问题并未涉及OPTIC API,而是通过仅对单个字符串进行操作来解决的。 How to convert string to date type in MarkLogic? 如何在MarkLogic中将字符串转换为日期类型? I need to take an optic "column" and convert its type to a date object so I can call the https://docs.marklogic.com/xdmp.monthNameFromDate and other related tools on it. 我需要使用一个光学“列”并将其类型转换为日期对象,以便可以在其上调用https://docs.marklogic.com/xdmp.monthNameFromDate和其他相关工具。

I feel missing something very straightforward about applying functions to row sets and selecting specific columns. 我觉得缺少一些将函数应用于行集和选择特定列的简单明了的东西。

What I naturally want to do is apply a function to each property of the resulting row set: 我自然想要做的是将一个函数应用于结果行集的每个属性:

let formatted = resulting_rows.map(x=>Date(x['completed date'])

or whatever. 管他呢。 This is basically what I do client side, but it feels incorrect to just throw away so much of the built-in javascript functionality and do this all in the browser, especially when I need to do groups on years and months from these views. 这基本上是我在客户端执行的操作,但是仅丢弃大量内置的javascript功能并在浏览器中完成所有操作,这是不正确的,尤其是当我需要根据这些视图进行数月或数月的分组时。

It doesn't help that some links about operating on objects are broken: https://docs.marklogic.com/map.keys 某些有关对对象进行操作的链接被破坏并没有帮助: https : //docs.marklogic.com/map.keys

The op.as() call defines a dynamic column based on an expression that's applied to each row when the query is executed. op.as()调用基于执行查询时应用于每行的表达式定义动态列。

The expression can only use calls to functions provided by the Optic API. 该表达式只能使用对Optic API提供的函数的调用。 In particular, where xs.date() executes when called, op.xs.date() executes when each row is processed. 特别是在调用xs.date()时执行,而处理每一行时则执行op.xs.date()。 Similarly fn.formatDate() executes immediately while op.fn.formatDate() executes during row processing. 类似地,fn.formatDate()立即执行,而op.fn.formatDate()在行处理期间执行。

To use the dynamic column, provide it as an argument to op.select(), similar to the following sketch: 要使用动态列,请将其作为op.select()的参数提供,类似于以下草图:

op.fromView('schema', 'money');
  .where(op.ne(op.col('completed date'), ""))
  .select([
     op.col('completed date'),
     op.as('testDate', op.fn.formatDate(
         xdmp.parseDateTime(
             op.col('completed date'),
             "[M01]/[D01]/[Y0001]"),
         "[M01]-[D01]-[Y0001]"))
     ])
  .result();

The call to .result() executes the query pipeline. 调用.result()将执行查询管道。

The map is an XQuery equivalent to JavaScript literal that's not used in sever-side JavaScript. 该映射是XQuery,等效于服务器端JavaScript中未使用的JavaScript文字。 Optic does support a map() pipeline step, which takes a lambda and appears in the pipeline step immediately before the call to result() as documented in: Optic确实支持map()管道步骤,该步骤需要一个lambda,并在调用result()之前紧接在管道步骤中出现,如下所示:

http://docs.marklogic.com/AccessPlan.prototype.map http://docs.marklogic.com/AccessPlan.prototype.map

Belated footnote: One alternative for this case to parsing and formatting the date would be to use op.fn.translate() to transform the column value by turning every instance of "/" into "-" 迟来的脚注:这种情况下,解析和格式化日期的另一种方法是使用op.fn.translate()通过将每个“ /”实例转换为“-”来转换列值。

Hoping that helps, 希望能有所帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM