简体   繁体   English

在服务器(nodejs)端处理json数据还是使用Couchdb的显示功能?

[英]Process json data at server (nodejs) side or using show function of couchdb?

I am using couchdb and nodejs for server side. 我在服务器端使用couchdb和nodejs。 I have a document with all transactions history array saved on couchdb. 我有一个文档,其中所有交易历史记录数组都保存在了沙发上。 The array can have multiple transactions with same trnxId with different status. 该阵列可以具有状态相同的相同trnxId的多个事务。 I need to filter the history based on some parameters and to fetch latest records but not deleted ones. 我需要根据一些参数过滤历史记录,并获取最新记录,但不要删除这些记录。

Doc in couchDB CouchDB中的文档

{
  "_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": []
  }
}

For filtering, i'm using below code in node js: 为了进行过滤,我在节点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;
};

Can i do the same processing in couchDB itself using Show function? 我可以使用Show函数在bedDB本身中进行相同的处理吗? I this case, i won't be able to use lodash functions. 在这种情况下,我将无法使用lodash函数。 Please guide me which way is better, either node js or Show function of couchDB? 请指导我哪种方法更好,是node js还是bedDB的Show函数?

To use lodash you may put lodash code as a string into, say, .lodash branch of your design document. 要使用lodash,您可以将lodash代码作为字符串放入设计文档的.lodash分支中。 Then you can use lodash inside your show function putting require("lodash") into show fn code. 然后,您可以在show函数中使用lodash,将require("lodash")放入show fn代码中。

Using show functions, however, implies some limitations: 但是,使用show函数意味着一些限制:

  • you only have 64Mb of RAM for any query server instance, so your processed doc can not be very long; 对于任何查询服务器实例,您只有64Mb的RAM,因此您处理的文档不能太长;

  • query server uses old SpiderMonkey engine, which means you have only ES5 onboard, so no arrow functions and other ES6 niceties; 查询服务器使用旧的SpiderMonkey引擎,这意味着您只有板载ES5,因此没有箭头功能和其他ES6优点;

  • built-in query server code (like shows/lists) tend to be painful for debugging. 内置的查询服务器代码(例如节目/列表)在调试时往往很麻烦。

It might be reasonable in some cases, like if you plan to distribute your code using sync mechanics, or do not want to decouple some of your biz logic bits from map/reduce code. 在某些情况下,这可能是合理的,例如,如果您计划使用同步机制来分发代码,或者不想将某些业务逻辑位与映射/减少代码分离。

However, in most cases if you have application layer, better use application layer. 但是,在大多数情况下,如果您具有应用程序层,则最好使用应用程序层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM