【问题标题】:Strapi update same field incrementStrapi 更新相同的字段增量
【发布时间】:2021-02-14 18:42:34
【问题描述】:

我有一个名为count 的字段,我想将该字段更新一定数量。是否可以在没有首先获得该字段的值的情况下在 Strapi 中进行操作?

类似这样的:

var myRating = 4;

strapi.query('post').update(
  { id: 6 },
  {
    rating: post.rating + myRating,
  }
);

【问题讨论】:

    标签: javascript strapi


    【解决方案1】:
    const result = await strapi
              .query("name of the table")
              .model.query((qb) => {
                qb.where("id", id);
                qb.increment("field", value);
              })
    
          .fetch();
    

    我用过这个,效果很好。

    任何正在研究此问题的人都可能想检查这些

    1. https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#custom-queries

    2. Set value = value +1 in bookshelf.js

    3. Bookshelf.js - Nested relations while querying with andWhere()

    【讨论】:

      【解决方案2】:

      默认情况下,您将无法使用 Strapi 提供的查询来执行此操作。 但是您可以通过本机 SQL 查询来完成。

      这里有一个StackOverflow question,它回答了请求做的事情。

      然后在 Strapi 中,您将必须获得 Knex to execute the query

      为此,您必须使用strapi.connections.default。它为您提供了 Knex 实例。

      【讨论】:

      • 使用 NoSQL 数据库怎么样?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多