sequelize postgres 将 fn 'date' 与 jsonb 值结合起来 [英] sequelize postgres combine fn 'date' with jsonb value

查看:24
本文介绍了sequelize postgres 将 fn 'date' 与 jsonb 值结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 sequelize 将 MM/DD/YYYY 格式的字符串与 postgres 中 jsonb 列上的另一个格式字符串进行比较.

I need to compare a string of format MM/DD/YYYY with another one in postgres on a jsonb column using sequelize.

在常规专栏中,我会做类似的事情

On a regular column I would do something like

sequelize.where(sequelize.fn('date', sequelize.col('created_at'), '>=', moment().subtract(1, 'days').format('MM/DD/YYY'))),

但是一旦值在 JSONB 中,我似乎无法找到引用它的方法.

but once the value is in JSONB I can't seem to find the way to reference it.

我尝试了多种变体:

sequelize.col("data ->> 'created_at'")

sequeliez.json("data.created_at")

等等……

实现这一点的正确方法是什么?

What is the right way to implement this?

推荐答案

我终于找到了一个方法是使用 literal

One way I finally found how to do this is using literal

sequelize.where(sequelize.fn('date', sequelize.literal(`data ->> 'created_at'`)), '>=', moment().subtract(1, 'days').format('MM/DD/YYY')))

这对我有用.

这篇关于sequelize postgres 将 fn 'date' 与 jsonb 值结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆