我需要等同于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;
}
};
答案 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
},
]
}