Skip to content
标签
欢迎扫码关注公众号

MongoDB 聚合函数

  1. count() 查询并返回匹配文档的个数

    bash
    > db.phones.count()
    316229
    > db.phones.count({'components.number': {$gt: 5599999}})
    172471
  2. distinct() 返回每个匹配的值(不是整个文档)

    bash
    > db.phones.distinct('components.number', {'components.number': {$lt: 5550005}})
  3. group() 类似于 SQL 中的 GROUP BY

    bash
    > db.phones.group({
    ... initial:{count: 0 },
    ... reduce:function(phone, output) { output.count++; },
    ... cond: {'components.number': {$gt: 5599999}},
    ... key: {'components.area' : true}
    ... })

    下面两个例子用来展示 group 函数的灵活性。

    • 3.1. 使用 group() 调用来重现 count() 函数的结果

      > db.phones.group({
          ... initial:{count: 0 },
          ... reduce:function(phone, output) { output.count++; },
          ... cond: {'components.number': {$gt: 5599999}}
          ... })
      
          > db.phones.count({'components.number': {$gt: 5599999}})
          172471
          ```
    • 3.2. 使用 group() 调用来重现 distinct() 函数的结果

      bash
      > db.phones.group({
      ... initial: { prefixes: {} },
      ... reduce: function(phone, output) {
      ... output.prefixes = 1;
      ... },
      ... finalize: function (out) {
      ... var ary = ;
      ... for(var p in out.prefixes) { ary.push(parseInt(p)); }
      ... out.prefixes = ary;
      ... }
      ... }).prefixes
      
      > db.phones.distinct('components.prefix')

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.