如何在 PouchDB 中使用动态字段对数据进行排序?

问题描述

我有这个查询来索引 first_name 并根据它对数据进行排序,它工作正常

try {
  Users.createIndex({
    index: { fields: ['first_name'] }
  }).then(function (response) {
    console.log(response);
  }).catch(function (err) {
    console.log(err);
  });
  const users = (await Users.find({
    limit,skip: limit * (page - 1),selector: {first_name: {$gt: null}},sort: [ { 'first_name' : 'asc'} ]
  })).docs;

但是当我尝试使用变量时它会触发错误

Error: Cannot sort on field(s) "orderBy" when using the default index

代码

orderBy = (query.params !== undefined && query.params.orderBy !== undefined) ? query.params.orderBy.sortField : 'first_name',sortOrder = (query.params !== undefined && query.params.orderBy !== undefined) ? query.params.orderBy.sortOrder : 'asc'

console.log('orderBy: ' + orderBy) // first_name
console.log('sortOrder: ' + sortOrder) // asc

try {
  Users.createIndex({
    index: { fields: [orderBy] }
  }).then(function (response) {
    console.log(response);
  }).catch(function (err) {
    console.log(err);
  });
  const users = (await Users.find({
    limit,selector: {orderBy: {$gt: null}},sort: [ { orderBy : sortOrder } ]
  })).docs;

如何编辑它以使其像静态变量一样使用动态变量?

解决方法

在下面的代码中,变量 orderBy 不会被替换

 selector: {orderBy: {$gt: null}},sort: [ { orderBy : sortOrder } ]

代码按字面计算 orderBy。要为对象分配动态键,请使用对象索引器:

myObject[myVar] = myVal;

因此在您的代码中,应该这样做。

const query = {
    selector: {},sort: []
  };
  // setup selector
  query.selector[prop] = {
    $gt: null
  };
  // setup sort
  let sortParam = {};
  sortParam[prop] = sortDirection;
  query.sort.push(sortParam);

我添加了一个 pouchDB 片段来说明这个概念。

let db;

// init example db instance
async function initDb() {

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

  await db.bulkDocs(getDocsToInstall());
}



initDb().then(async() => {
  await db.createIndex({
    index: {
      fields: ['first_name']
    }
  });
  await doQuery("first_name","desc");
});

async function doQuery(prop,sortDirection) {
  const query = {
    selector: {},sort: []
  };
  // setup selector
  query.selector[prop] = {
    $gt: null
  };
  // setup sort
  let sortParam = {};
  sortParam[prop] = sortDirection;
  query.sort.push(sortParam);
  // log the query
  console.log(JSON.stringify(query,undefined,3));
  // exec the query
  const users = (await db.find(query)).docs;
  users.forEach(d => console.log(d[prop]));
  
}
// canned test documents
function getDocsToInstall() {
  return [{
      first_name: "Jerry"
    },{
      first_name: "Bobby"
    },{
      first_name: "Phil"
    },{
      first_name: "Donna"
    },{
      first_name: "Ron"
    },{
      first_name: "Mickey"
    },{
      first_name: "Bill"
    },{
      first_name: "Tom"
    },{
      first_name: "Keith"
    },{
      first_name: "Brent"
    },{
      first_name: "Vince"
    },]
}
<script src="https://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.min.js"></script>