【发布时间】:2017-11-08 11:01:32
【问题描述】:
我在服务器端使用 couchdb 和 nodejs。 我有一个文档,其中包含所有交易历史记录数组保存在 couchdb 上。该数组可以有多个具有相同 trnxId 且具有不同状态的事务。我需要根据一些参数过滤历史记录并获取最新记录但不删除。
沙发数据库中的文档
{
"_id": "hist",
"_rev": "3-62da7472a24652fd5cf42e01bcef294f",
"type": "history",
"status": "ACTIVE",
"accounts": {
"payment": [
{
"trnxId": "5784365893938",
"trnxDate": "2017-11-07T06:12:15.000Z",
"trnxMode": "cash",
"amount": 50,
"trnxStatus": "new",
"accName": "Cash",
"category": "others",
"comments": "no comments"
}
],
"expense": [],
"transfer": []
}
}
为了过滤,我在节点 js 中使用以下代码:
var __ = require('lodash');
function fetchAllHist(docObj, trnxType) {
var trnxArray = docObj.accounts[trnxType];
var histArray = [];
for (var i = trnxArray.length - 1; i >= 0; i--) { //inverse loop as trnxArray is having data in ascending order over creation date
var trnxId = trnxArray[i].trnxId;
if (__.findIndex(histArray, { 'trnxId': trnxId }) == -1) {
histArray.push(trnxArray[i]);
}
}
histArray = __.reject(histArray, { 'trnxStatus': "deleted" });
return histArray;
};
我可以使用 Show 函数在 couchDB 本身中进行相同的处理吗?在这种情况下,我将无法使用 lodash 函数。 请指导我哪种方式更好,节点js或couchDB的显示功能?
【问题讨论】:
-
你尝试过 Mango 或 Map/Reduce 吗?
-
没有。因为如果有超过 1 个文档要处理并获取有限的编号,Mango 和 Map/reduce 会很有帮助。数据/字段。这里的情况完全不同。我想处理驻留在单个文档中的 json 对象数组,预期结果应该是 json 数组。使用 mango 查询或 map/reduce 是不可能的。如果我错了,请纠正我。
-
你能用 Show 功能做到这一点吗?大概。最好的选择是什么?我认为您应该只在应用程序层中处理它。根据我的阅读,不建议使用显示/列表功能
-
谢谢亚历克西斯!您能否分享您提到的链接(“我所阅读的内容不建议使用显示/列表功能”)
-
看看这个 Joan Touzet 的presentation
标签: javascript arrays json node.js couchdb