简体   繁体   English

在不将整个集合加载到DerbyJS 0.6中的情况下获取文档计数

[英]Get count of documents without loading the whole collection in DerbyJS 0.6

How can I count the results of a query without loading the whole resultset into memory? 如何在不将整个结果集加载到内存的情况下计算查询结果?

The easy way of counting documents returned by a query would be: 计数查询返回的文档的简单方法是:

var q = model.query('mycollection', { date: today });
q.fetch(function() {
    var length = q.get().length;
});

But this would load the whole resultset into memory and "count" an array in javascript. 但这会将整个结果集加载到内存中,并在javascript中“计数”一个数组。 When you have lots of data you don't want to do this. 当您有大量数据时,您不想这样做。 I think. 我认为。

Counting the underlying mongodb collection is rather complicated since LiveDB (I think it is LiveDB) creates many mongodb documents for one derbyjs document. 计算底层mongodb集合非常复杂,因为LiveDB(我认为是LiveDB)为一个derbyjs文档创建了许多mongodb文档。

The internets point to this google groups thread from 2013, but the solution described there (putting $count: true into the query options) doesn't seem to work in DerbyJS 0.6 and current mongodb.". query.extraRef is undefined . 互联网指向2013年以来的这个Google网上论坛线程 ,但是那里描述的解决方案(将$count: true放入查询选项)在DerbyJS 0.6和当前的mongodb中似乎不起作用。“。 query.extraRefundefined

It is done like described in the google groups thread. 就像google groups线程中所述的那样完成。 But query.extraRef is now query.refExtra . 但是query.extraRef现在是query.refExtra

Example: 例:

var q = model.query('mycollection', { $count: true, date: today });
q.refExtra('_page.docsOfToday');
q.fetch();

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

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