Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用knexjs更新json列中具有值的对象的键?_Node.js_Knex.js - Fatal编程技术网

Node.js 如何使用knexjs更新json列中具有值的对象的键?

Node.js 如何使用knexjs更新json列中具有值的对象的键?,node.js,knex.js,Node.js,Knex.js,我正在尝试更新用户表中的一列,列类型为json 列名为test 列由一个对象组成,例如默认值为 {a:“文本”,b:0} 如何在不更改整个列的情况下更新对象键b 我使用的代码是 knexDb('users').where({ email: email }) .update({ test: { b: 1 } }) 第二种解决方案 knexDb('users').where({ email: email }) .update({ test: knexDb.raw(`jsonb_se

我正在尝试更新用户表中的一列,列类型为json

列名为test

列由一个对象组成,例如默认值为

{a:“文本”,b:0}

如何在不更改整个列的情况下更新对象键
b

我使用的代码是

knexDb('users').where({
  email: email
})
.update({
  test: { b: 1 }
})
第二种解决方案

knexDb('users').where({
  email: email
})
.update({
  test: knexDb.raw(`jsonb_set(??, '{b}', ?)`, ['test', 1])
})
第一个解决方案更改整个列单元格,测试将仅为
{b:1}

第二个解决方案不起作用,请给出一个错误

函数jsonb_集(json,未知,未知)不存在

预期结果

只更新对象中的某个键值,而不更改整个对象

PS

例如,我还想更新由上述对象组成的数组

[{a:“文本”,b:0},{c:“另一文本”,d:0}]

如果我在kenxjs中使用上面的代码,它会将整个数组更新为仅
{b:1}

PS在搜索了很多内容后,我发现为了使它工作,我需要将column type设置为jsonb,以便上面的jsonb_set()工作

但现在我面临另一个问题

如何使用jsonb_集更新多个键


第一个查询键b现在没有更新,事实上,除了最后一个查询键
a
,所有更新都不起作用,所以可以解释一下原因吗?

您的问题是您正在覆盖
测试。您传递到
update
的是一个JS对象()。不能有多个值相同的键()。您必须这样做,将所有原始SQL作为
test
的值,生成一个长字符串

knexDb('users').where({
      email: email
    })
    .update({
      test: knexDb.raw(`
          jsonb_set(??, '{a}', ?)
          jsonb_set(??, '{b}', ?)
        `,
        ['test', "another-text", 'test', 1])
    })
可能存在一个更好的选择——如果您必须对多个列执行此操作,那么它的可读性会更高,这与我在下面介绍的内容类似。在本例中,包含
jsonb
的列称为
json

const updateUser = async (email, a, b) => {

  const user = await knexDb('users')
    .where({ email })
    .first();

  user.json.a = a;
  user.json.b = b;

  const updatedUser = await knexDb('users')
    .where({ email })
    .update(user)
    .returning('*');

  return updatedUser;
}

您的问题是您正在覆盖
test
。您传递到
update
的是一个JS对象()。不能有多个值相同的键()。您必须这样做,将所有原始SQL作为
test
的值,生成一个长字符串

knexDb('users').where({
      email: email
    })
    .update({
      test: knexDb.raw(`
          jsonb_set(??, '{a}', ?)
          jsonb_set(??, '{b}', ?)
        `,
        ['test', "another-text", 'test', 1])
    })
可能存在一个更好的选择——如果您必须对多个列执行此操作,那么它的可读性会更高,这与我在下面介绍的内容类似。在本例中,包含
jsonb
的列称为
json

const updateUser = async (email, a, b) => {

  const user = await knexDb('users')
    .where({ email })
    .first();

  user.json.a = a;
  user.json.b = b;

  const updatedUser = await knexDb('users')
    .where({ email })
    .update(user)
    .returning('*');

  return updatedUser;
}

您使用的是什么数据库?@felixmosh psql如果您使用的是
json
而不是
jsonb
,则必须将其拉出、解析、更改、字符串化,然后进行更新。看到你在用什么数据库了吗?@felixmosh psql如果你用的是
json
而不是
jsonb
,你必须把它拉出来,解析,修改,字符串化,然后更新它。看见