pouchDB 并按日期或日期范围查询

pouchDB and querying by date or date range

我目前能够成功地从我的数据库中获取数据,创建索引,然后在 react.js 渐进式网络应用程序中使用 pouchDB 获取最新的 7 条记录。

我目前的障碍是按日期查询。因此,使用我当前的代码,我获取了所有数据,创建了一个索引,然后我只获取了 7 条最新记录,以便我可以映射数据。但我想弄清楚如何使用当天的时刻日期:newestDate: moment(this.currentDate).format("YYYY-MM-DD") 并用它来表示 "get the most recent record from each day over the last 7 days"

如此有效,我有格式为 {caloriesBurned:"250", caloriesConsumed:"1450", createdAt:"2020-03-05"} 的记录,我希望能够修改我的查询以每天只获取一条记录,从今天开始并返回 7 天

如何使用以下代码在 pouchDB 中正确执行此操作:

fetchData = () => {
    this.setState({
        calorieElements: null,
    });
    this.state.caloriesDB.db.allDocs({
        include_docs: true,
    }).then(result => {
        const rows = result.rows;
        console.log('this is a row');
        console.log(result);
        this.setState({
            calorieElements: rows.map(row => row.doc),
        });
        console.log(this.state.calorieElements);
    }).catch((err) =>{
        console.log(err);
    });
  }

  getMax = () => {

    this.state.caloriesDB.db.createIndex({
      index: {
        fields: ['_id','caloriesBurned', 'createdAt']
      }
    }).then(result => {
      console.log(result);
      this.setMax();

   }).catch((err) =>{
      console.log(err);
    });
  }

  setMax = () => {
    this.state.caloriesDB.db.find({
      selector: {
        $and: [
          {_id: {"$gte": null}},
          {caloriesBurned: {$exists: true}},
          {caloriesConsumed: {$exists: true}},
          {createdAt: {$exists: true}}
        ]
      },
      fields: ['caloriesBurned','caloriesConsumed', 'createdAt'],
      sort: [{'_id':'desc'}],
      limit: 7
    }).then(result => {

      const newDocs = result.docs;
      const docCalories = newDocs.map(x => +x.caloriesConsumed - +x.caloriesBurned);

    }).catch((err) =>{
      console.log(err);
    });
  }

这里有一个例子,说明了实现您的愿望的可靠方法。我已将代码尽可能接近您的代码。关键是索引字段的顺序,createdAt排在第一位方便排序。

let db;
// demo 
initDb().then(() => {
  // get last 7 days on 2020-03-09
  return queryCreatedAt("2020-03-09", 7, 'desc');
}).then(result => {
  // display results.
  console.log(result);
})

// query docs by createdAt 
function queryCreatedAt(date, limit, order) {
  return db.find({
    selector: {
      $and: [{
          createdAt: {
            $lte: date
          }
        },
        {
          caloriesBurned: {
            $exists: true
          }
        },
        {
          caloriesConsumed: {
            $exists: true
          }
        }
      ]
    },
    sort: [{
      createdAt: order
    }],
    limit: limit,
    fields: [
      'createdAt',
      'caloriesBurned',
      'caloriesConsumed'
    ]
  });
}

// init db instance
function initDb() {

  db = new PouchDB('test', {
    adapter: 'memory'
  });

  return db.bulkDocs(getDocsToInstall()).then(() => {
    return db.createIndex({
      index: {
        fields: [
          'createdAt',
          'caloriesBurned',
          'caloriesConsumed'
        ]
      }
    })
  })
}

// get test docs
function getDocsToInstall() {
  return [{
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-02-27"
    }, {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-02-28"
    }, {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-02-29"
    },
    {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-03-01"
    }, {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-03-02"
    },
    {
      "caloriesBurned": "950",
      "caloriesConsumed": "1950",
      "createdAt": "2020-03-03"
    },
    {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-03-04"
    },
    {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-03-05"
    },
    {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": "2020-03-06"
    },
    {
      "caloriesBurned": "450",
      "caloriesConsumed": "1450",
      "createdAt": "2020-03-07"
    },
    {
      "caloriesBurned": "550",
      "caloriesConsumed": "1550",
      "createdAt": "2020-03-08"
    }
  ]
}
<script src="//cdn.jsdelivr.net/npm/pouchdb@7.1.1/dist/pouchdb.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.find.js"></script>

为了在同一日期支持多个条目,我会 非规范化 呈现给基于天(24 小时)的文档的单个文档,条目在这样的数组中。我更喜欢使用 UTC 而不是字符串日期 - 这是个人选择。

{
  entries: [
    {
      "caloriesBurned": "350",
      "caloriesConsumed": "1350",
      "createdAt": 1586357069
    },
    {
      "caloriesBurned": "450",
      "caloriesConsumed": "1450",
      "createdAt": 1586357080
    },
    {
      "caloriesBurned": "550",
      "caloriesConsumed": "1550",
      "createdAt": 1586357140
    }
  ]
}

有关如何索引和查询集合中的项目的灵感,请参阅 SO 文章