首页 > 解决方案 > LokiJs 集合上的芒果查询更改了集合的大小

问题描述

我将 LokiJs 用作应用程序的内存缓存,该应用程序将数据存储在 CouchDB 中并允许通过 NodeJS 服务器进行检索。根据有关构建查询的文档,我的查询结果被分配给一个结果集对象:

let result = combinedCache.find({'left.errorCode': errorCode});

combinedCache集合是.eqJoin其他两个集合的结果:

    var cache = new loki('loki.db');

    errorCodeCache = cache.addCollection('errorCodes');
    messageCache = cache.addCollection('messages');
    errorCodeCache.insert(docs.errorCodes);
    messageCache.insert(docs.messages);
    combinedCache = errorCodeCache.eqJoin(
        messageCache,
        'messageId',
        '_id'
    );

这将创建两个集合,一个errorCodeCache集合和一个messageCache集合。这combinedCache是错误messageId代码文档和_id消息文档的连接结果。当combinedCache我使用combinedCache.data().length.

但是,在我对缓存执行查询后,原始集合的大小更改为结果的大小;换句话说,如果我的查询返回 1 个 CouchDB 文档,combinedCache则 的大小现在为 1。我在文档中找不到任何引用说明.find对集合的查询会改变集合本身的结果。如果是这种情况,如何保留集合的大小?谢谢!

标签: node.jslokijs

解决方案


对于看到这一点的其他人,我设法使用这篇文章弄清楚了。

// branch just long enough to determine count of customers under 30 without filter affecting irishCustomers results
var lt30count = irishCustomers.copy().find({ ‘age’: { ‘$lt’ : 30 }).data().length;

所以解决方法是.copy()如果不希望过滤器修改集合,就添加创建数据的临时副本。


推荐阅读