<small id='WDbxR'></small><noframes id='WDbxR'>

    1. <i id='WDbxR'><tr id='WDbxR'><dt id='WDbxR'><q id='WDbxR'><span id='WDbxR'><b id='WDbxR'><form id='WDbxR'><ins id='WDbxR'></ins><ul id='WDbxR'></ul><sub id='WDbxR'></sub></form><legend id='WDbxR'></legend><bdo id='WDbxR'><pre id='WDbxR'><center id='WDbxR'></center></pre></bdo></b><th id='WDbxR'></th></span></q></dt></tr></i><div id='WDbxR'><tfoot id='WDbxR'></tfoot><dl id='WDbxR'><fieldset id='WDbxR'></fieldset></dl></div>

        <bdo id='WDbxR'></bdo><ul id='WDbxR'></ul>
      <tfoot id='WDbxR'></tfoot><legend id='WDbxR'><style id='WDbxR'><dir id='WDbxR'><q id='WDbxR'></q></dir></style></legend>
    2. 如何计算猫鼬中具有一个不同字段的记录?

      时间:2023-08-08
        <tfoot id='oGqKL'></tfoot>

              <tbody id='oGqKL'></tbody>
          • <small id='oGqKL'></small><noframes id='oGqKL'>

            <legend id='oGqKL'><style id='oGqKL'><dir id='oGqKL'><q id='oGqKL'></q></dir></style></legend>

            <i id='oGqKL'><tr id='oGqKL'><dt id='oGqKL'><q id='oGqKL'><span id='oGqKL'><b id='oGqKL'><form id='oGqKL'><ins id='oGqKL'></ins><ul id='oGqKL'></ul><sub id='oGqKL'></sub></form><legend id='oGqKL'></legend><bdo id='oGqKL'><pre id='oGqKL'><center id='oGqKL'></center></pre></bdo></b><th id='oGqKL'></th></span></q></dt></tr></i><div id='oGqKL'><tfoot id='oGqKL'></tfoot><dl id='oGqKL'><fieldset id='oGqKL'></fieldset></dl></div>

                <bdo id='oGqKL'></bdo><ul id='oGqKL'></ul>
                本文介绍了如何计算猫鼬中具有一个不同字段的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在为 nodejs 探索 mongoose 时,我遇到了需要知道我的集合中用户数量的问题:

                While exploring mongoose for nodejs I ran into the problem of needing to know the amount of user in my collection:

                我的收藏有记录,每条记录都有一个用户.我想知道唯一(不同)用户的数量.

                My collection has records, each record has a user. I want to know the amount of unique (different) users.

                我怎样才能用猫鼬做到这一点?

                How can I do this with mongoose?

                数据库增长很快,有没有办法从数据库中取回数字而不是获取所有不同的记录并计算它们?

                The database is growing quite fast, is there anyway to get the number back from the DB instead of getting all the distinct records and counting them?

                推荐答案

                这是一个替代答案,因为当我尝试 Reddest 的 Mongoose 3.1.2 方法时遇到异常(这对我来说似乎是 Mongoose 中的一个错误,因为 Reddest 的方法应该没事).

                Here's an alternative answer as I get an exception when I try Reddest's approach with Mongoose 3.1.2 (which seems like a bug in Mongoose to me as Reddest's approach should be fine).

                您可以在集合的模型上调用 distinct 方法,指定该集合的用户标识字段的名称:

                You can call the distinct method on your collection's model, specifying the name of the user-identifying field of that collection:

                Record.distinct('user_id').exec(function (err, user_ids) {
                    console.log('The number of unique users is: %d', user_ids.length);
                });
                

                或者,如果您想从查找中链接 distinct 调用,请在 distinct 调用中包含回调(这对我有用):

                Or if you want to chain the distinct call from a find, include the callback in the distinct call (this did work for me):

                Record.find().distinct('user_id', function (err, user_ids) { ... });
                

                更新

                如果您只想要计数而不获取值,请在链中添加 count() 调用:

                If you just want the count without getting the values, stick a count() call in the chain:

                Record.distinct('user_id').count().exec(function (err, count) {
                    console.log('The number of unique users is: %d', count);
                });
                

                注意:这在最新的 Mongoose 代码 (3.5.2) 中不起作用.

                NOTE: this doesn't work in the latest Mongoose code (3.5.2).

                这篇关于如何计算猫鼬中具有一个不同字段的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Dynamodb 过滤器表达式不返回所有结果 下一篇:如何在 PouchDB 上模拟聚合函数 avg、sum、max、min

                相关文章

                最新文章

              • <tfoot id='zjUjS'></tfoot>

                <small id='zjUjS'></small><noframes id='zjUjS'>

                  <bdo id='zjUjS'></bdo><ul id='zjUjS'></ul>
              • <i id='zjUjS'><tr id='zjUjS'><dt id='zjUjS'><q id='zjUjS'><span id='zjUjS'><b id='zjUjS'><form id='zjUjS'><ins id='zjUjS'></ins><ul id='zjUjS'></ul><sub id='zjUjS'></sub></form><legend id='zjUjS'></legend><bdo id='zjUjS'><pre id='zjUjS'><center id='zjUjS'></center></pre></bdo></b><th id='zjUjS'></th></span></q></dt></tr></i><div id='zjUjS'><tfoot id='zjUjS'></tfoot><dl id='zjUjS'><fieldset id='zjUjS'></fieldset></dl></div>
                  <legend id='zjUjS'><style id='zjUjS'><dir id='zjUjS'><q id='zjUjS'></q></dir></style></legend>