• MongoDB学习笔记05


    count 返回集合中文档数量文档数量

    db.foo.count()
    db.foo.count({"x":2})
    View Code

    distinct用来找出给定键的所有不同的值,使用时必须指定集合和键

    db.runCommand({"distinct":"person","key","age"})
    db.person.distinct("age")
    View Code

    group的聚合要先选定分组所依据的键【键要是个对象才能返回所有的分组结果】,而后MongoDB就会将集合依据选定键值的不同分成若干组  condition 满足指定条件的文档

    1 db.runCommand({"group":{ "ns":"stocks", "key":{"day":true}, "initial":{"time":0}, "$reduce":function(doc,prev){   if(d
    2 oc.time>prev.time){     prev.price=doc.price;     prev.time=doc.time; } }}})
    3 
    4 db.runCommand({"group":{ "ns":"stocks", "key":"day", "initial":{"time":0}, "$reduce":function(doc,prev){   if(d
    5 oc.time>prev.time){     prev.price=doc.price;     prev.time=doc.time; } }}})
    View Code

    ps:ns 指定要进行分组的集合 key 指定分组依据的键 initial 每一组reduce函数调用的初始时间,会作为初始文档传递给后续过程  $reduce 每个文档都对应一次这个调用

    结果:

     1 {
     2         "retval" : [
     3                 {
     4                         "day" : "2010/1/1",
     5                         "time" : "1",
     6                         "price" : 1
     7                 },
     8                 {
     9                         "day" : "2010/1/2",
    10                         "time" : "1",
    11                         "price" : 1
    12                 },
    13                 {
    14                         "day" : "2010/1/3",
    15                         "time" : "1",
    16                         "price" : 1
    17                 }
    18         ],
    19         "count" : NumberLong(4),
    20         "keys" : NumberLong(3),
    21         "ok" : 1
    22 }
    23 
    24 
    25 {
    26         "retval" : [
    27                 {
    28                         "time" : "1",
    29                         "price" : 1
    30                 }
    31         ],
    32         "count" : NumberLong(4),
    33         "keys" : NumberLong(1),
    34         "ok" : 1
    35 }
    View Code
  • 相关阅读:
    ZOJ Bookcase
    C*++ Calculations
    STL <cctype>
    线段树单点更新+区间更新
    ZOJ Supermarket
    STL <cassert>
    算法导论<二>
    MV Maker [DP]
    LIS 最长有序子序列(递增/递减/非递增/非递减)
    LIS
  • 原文地址:https://www.cnblogs.com/goodlucklzq/p/4384939.html
Copyright © 2020-2023  润新知