【问题标题】:Azure cosmos db continuation token not working with WHEREAzure cosmos db 延续令牌不适用于 WHERE
【发布时间】:2021-11-02 21:02:07
【问题描述】:

我有一些润滑油。润滑油可能有货,也可能缺货。我想一次最多查询 5 个润滑油。

当我不使用过滤器时,会返回继续令牌并正常工作。

// <Get all lubes / first page (max: 5) >
  async getAll(token?: string, limit = 5) {
    if (!token) this.store.lube.removeAll();
    const {
      resources: items,
      continuationToken,
      hasMoreResults,
    } = await this.dbContainer.items
      .readAll<ILube>({
        maxItemCount: limit,
        continuationToken: token,
        // partitionKey: "HFO",
      })
      .fetchNext();
    this.store.lube.load(items);
    return { hasMoreResults, continuationToken };
  }

但是,当我想使用 WHERE 按可用润滑油或缺货进行过滤时,会返回继续令牌,但是,当我检索数据时,它不会t 工作 - 没有错误。

// <Get by availability status>
  async getByAvailability(availability: boolean, token?: string, limit = 5) {
    if (!token) this.store.lube.removeAll();
    this.store.lube.removeAll();
    const querySpec = {
      query: "SELECT * FROM c WHERE c.isAvailable = @availability",
      parameters: [
        {
          name: "@availability",
          value: availability,
        },
      ],
    };
    const {
      resources: items,
      continuationToken,
      hasMoreResults,
    } = await this.dbContainer.items
      .query<ILube>(querySpec, { maxItemCount: limit })
      .fetchNext();
    this.store.lube.load(items);
    return { hasMoreResults, continuationToken };
  }

底线,我如何将continuation tokenwhere 一起使用?

【问题讨论】:

标签: azure azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

代码中存在错误。延续令牌可与WHERE 一起使用。

错误1:我没有在getByAvailability的查询中包含continuationToken

错误 2:即使在 token!=null 时,我也将商品从商店中移除。

this.store.lube.removeAll();

// <Get by availability status>
  async getByAvailability(availability: boolean, token?: string, limit = 5) {
    if (!token) this.store.lube.removeAll();


    // **Mistake 2** 
    // this.store.lube.removeAll(); // This line is not needed.

    const querySpec = {
      query: "SELECT * FROM c WHERE c.isAvailable = @availability",
      parameters: [
        {
          name: "@availability",
          value: availability,
        },
      ],
    };


   **Mistake 1: Query didn't have continuationToken in it, only had the one being returned.**
    
    const {
      resources: items,
      continuationToken,
      hasMoreResults,
    } = await this.dbContainer.items
      .query<ILube>(querySpec, { maxItemCount: limit, continuationToken: token })
      .fetchNext();
    this.store.lube.load(items);
    return { hasMoreResults, continuationToken };
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2019-09-17
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多