pouchDB与列不同

时间:2014-12-19 18:34:24

标签: pouchdb

我需要等同于select distinct(toc.content)

的表中的acModel='7X';

这是我的文件 -

{
    acModel : "7X",
    relYear : 2014,
    optId : 11046,
    optNum : "2.386.ADW0",
    content : "Interior",
    cutInFrom : 253,
    cutInTo : 9999,
    weight : "Standard"
}

以下是javascript函数 -

createIndex: function () {
    this.createDesignDoc('by_acModel', function (doc) {
        emit(doc.acModel, doc.content);
    });
},
getDistinctContent: function(acModel){
    var result=[];
    function map(opt) {
        emit(opt.acModel, {content: content});
    }
    this.db.query(map,
        function (err, response) {
            for (var i = 0; i < response.total_rows; ++i) {
                if (response.rows[i].key == acModel) {
                     //Over here I am not sure ... Short of handling it manually, I don't know if I can get pouchDB to give distinct
                }
            }
    });
},
createDesignDoc: function(name, mapFunction) {
    var ddoc = {
        _id: '_design/' + name,
        views: {
        }
    };
    ddoc.views[name] = { map: mapFunction.toString() };
    return ddoc;
}
};

1 个答案:

答案 0 :(得分:1)

您可以使用_count作为设计文档的reduce函数,然后在使用{reduce: true, group: true}查询时,您将获得每个唯一colB以及他们的数量。

var db = new PouchDB('mydb');

function createDesignDoc(name, mapFunction, reduceFunction) {
    var ddoc = {
        _id: '_design/' + name,
        views: {
        }
    };
    ddoc.views[name] = { 
      map: mapFunction.toString(),
      reduceFunction: reduceFunction.toString()
    };
    return ddoc;
}

var ddoc = createDesignDoc(
  'myview', 
  function (doc) { emit(doc.colB); }, 
  '_count')
);

return db.put(ddoc).catch(function (err) {
  if (err.status !== 409) { // 409 is conflict
    throw err;
  }
}).then(function () {
  return db.query('myview', {
    reduce: true,
    group: true
  });
});

你会得到类似的东西:

{
  rows: [
    {
      key: 'foo',
      value: 3    
    },
    {
      key: 'bar',
      value: 1   
    },
  ]
}