Mongodb学习笔记四(Mongodb聚合函数)

第四章 Mongodb聚合函数


插入 测试数据

for(var j=1;j<3;j++){
for(var i=1;i<3;i++){
  var person={
    Name:"jack"+i,
    Age:i,
    Address:["henan","wuhan"],
    Course:[
    {Name:"shuxue",Score:i},
    {Name:"wuli",Score:i}
    ]
  }
  db.DemoTest.Person.insert(person)
}
}

Count

db.DemoTest.Person.count({Name:"jack1"})

返回数量

distinct

db.DemoTest.Person.distinct("Name")

返回不重复的Name值。

group

例子:按照Name分组,条件是Age大于46

db.DemoTest.Person.group({
    "key":{"Name":true}, -----分组的keky
    "initial":{"Person":[]},-------每组分享的一个”初始化函数“
    "$reduce":function(cur,prev){   ------这个函数的第一个参数是当前的文档对象,第二个参数是上一次function操作的累计对象,第一次为initial中的{”person“:[]}。有多少个文档, $reduce就会调用多少次

        prev.Person.push(cur);        
},
    "finalize":function(prev){   ---返回每组的数量
      prev.count=prev.Person.length;
    },
    "condition":{"Age":{"$lt":46}}   -----过滤条件
    })

返回结果如下:

/* 0 */
{
    "0" : {
        "Name" : "jack1",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9d6"),
                "Name" : "jack1",
                "Age" : 1,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 1
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 1
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba07"),
                "Name" : "jack1",
                "Age" : 1,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 1
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 1
                    }
                ]
            }
        ],
        "count" : 2
    },
    "1" : {
        "Name" : "jack2",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9d7"),
                "Name" : "jack2",
                "Age" : 2,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 2
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 2
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba08"),
                "Name" : "jack2",
                "Age" : 2,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 2
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 2
                    }
                ]
            }
        ],
        "count" : 2
    },
    "2" : {
        "Name" : "jack3",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9d8"),
                "Name" : "jack3",
                "Age" : 3,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 3
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 3
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba09"),
                "Name" : "jack3",
                "Age" : 3,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 3
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 3
                    }
                ]
            }
        ],
        "count" : 2
    },
    "3" : {
        "Name" : "jack4",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9d9"),
                "Name" : "jack4",
                "Age" : 4,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 4
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 4
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0a"),
                "Name" : "jack4",
                "Age" : 4,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 4
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 4
                    }
                ]
            }
        ],
        "count" : 2
    },
    "4" : {
        "Name" : "jack5",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9da"),
                "Name" : "jack5",
                "Age" : 5,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 5
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 5
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0b"),
                "Name" : "jack5",
                "Age" : 5,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 5
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 5
                    }
                ]
            }
        ],
        "count" : 2
    },
    "5" : {
        "Name" : "jack6",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9db"),
                "Name" : "jack6",
                "Age" : 6,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 6
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 6
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0c"),
                "Name" : "jack6",
                "Age" : 6,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 6
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 6
                    }
                ]
            }
        ],
        "count" : 2
    },
    "6" : {
        "Name" : "jack7",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9dc"),
                "Name" : "jack7",
                "Age" : 7,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 7
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 7
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0d"),
                "Name" : "jack7",
                "Age" : 7,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 7
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 7
                    }
                ]
            }
        ],
        "count" : 2
    },
    "7" : {
        "Name" : "jack8",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9dd"),
                "Name" : "jack8",
                "Age" : 8,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 8
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 8
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0e"),
                "Name" : "jack8",
                "Age" : 8,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 8
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 8
                    }
                ]
            }
        ],
        "count" : 2
    },
    "8" : {
        "Name" : "jack9",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9de"),
                "Name" : "jack9",
                "Age" : 9,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 9
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 9
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba0f"),
                "Name" : "jack9",
                "Age" : 9,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 9
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 9
                    }
                ]
            }
        ],
        "count" : 2
    },
    "9" : {
        "Name" : "jack10",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9df"),
                "Name" : "jack10",
                "Age" : 10,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 10
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 10
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba10"),
                "Name" : "jack10",
                "Age" : 10,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 10
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 10
                    }
                ]
            }
        ],
        "count" : 2
    },
    "10" : {
        "Name" : "jack11",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e0"),
                "Name" : "jack11",
                "Age" : 11,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 11
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 11
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba11"),
                "Name" : "jack11",
                "Age" : 11,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 11
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 11
                    }
                ]
            }
        ],
        "count" : 2
    },
    "11" : {
        "Name" : "jack12",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e1"),
                "Name" : "jack12",
                "Age" : 12,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 12
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 12
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba12"),
                "Name" : "jack12",
                "Age" : 12,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 12
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 12
                    }
                ]
            }
        ],
        "count" : 2
    },
    "12" : {
        "Name" : "jack13",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e2"),
                "Name" : "jack13",
                "Age" : 13,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 13
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 13
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba13"),
                "Name" : "jack13",
                "Age" : 13,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 13
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 13
                    }
                ]
            }
        ],
        "count" : 2
    },
    "13" : {
        "Name" : "jack14",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e3"),
                "Name" : "jack14",
                "Age" : 14,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 14
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 14
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba14"),
                "Name" : "jack14",
                "Age" : 14,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 14
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 14
                    }
                ]
            }
        ],
        "count" : 2
    },
    "14" : {
        "Name" : "jack15",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e4"),
                "Name" : "jack15",
                "Age" : 15,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 15
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 15
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba15"),
                "Name" : "jack15",
                "Age" : 15,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 15
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 15
                    }
                ]
            }
        ],
        "count" : 2
    },
    "15" : {
        "Name" : "jack16",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e5"),
                "Name" : "jack16",
                "Age" : 16,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 16
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 16
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba16"),
                "Name" : "jack16",
                "Age" : 16,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 16
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 16
                    }
                ]
            }
        ],
        "count" : 2
    },
    "16" : {
        "Name" : "jack17",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e6"),
                "Name" : "jack17",
                "Age" : 17,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 17
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 17
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba17"),
                "Name" : "jack17",
                "Age" : 17,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 17
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 17
                    }
                ]
            }
        ],
        "count" : 2
    },
    "17" : {
        "Name" : "jack18",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e7"),
                "Name" : "jack18",
                "Age" : 18,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 18
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 18
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba18"),
                "Name" : "jack18",
                "Age" : 18,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 18
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 18
                    }
                ]
            }
        ],
        "count" : 2
    },
    "18" : {
        "Name" : "jack19",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e8"),
                "Name" : "jack19",
                "Age" : 19,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 19
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 19
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba19"),
                "Name" : "jack19",
                "Age" : 19,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 19
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 19
                    }
                ]
            }
        ],
        "count" : 2
    },
    "19" : {
        "Name" : "jack20",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9e9"),
                "Name" : "jack20",
                "Age" : 20,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 20
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 20
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1a"),
                "Name" : "jack20",
                "Age" : 20,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 20
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 20
                    }
                ]
            }
        ],
        "count" : 2
    },
    "20" : {
        "Name" : "jack21",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ea"),
                "Name" : "jack21",
                "Age" : 21,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 21
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 21
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1b"),
                "Name" : "jack21",
                "Age" : 21,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 21
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 21
                    }
                ]
            }
        ],
        "count" : 2
    },
    "21" : {
        "Name" : "jack22",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9eb"),
                "Name" : "jack22",
                "Age" : 22,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 22
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 22
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1c"),
                "Name" : "jack22",
                "Age" : 22,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 22
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 22
                    }
                ]
            }
        ],
        "count" : 2
    },
    "22" : {
        "Name" : "jack23",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ec"),
                "Name" : "jack23",
                "Age" : 23,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 23
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 23
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1d"),
                "Name" : "jack23",
                "Age" : 23,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 23
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 23
                    }
                ]
            }
        ],
        "count" : 2
    },
    "23" : {
        "Name" : "jack24",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ed"),
                "Name" : "jack24",
                "Age" : 24,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 24
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 24
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1e"),
                "Name" : "jack24",
                "Age" : 24,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 24
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 24
                    }
                ]
            }
        ],
        "count" : 2
    },
    "24" : {
        "Name" : "jack25",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ee"),
                "Name" : "jack25",
                "Age" : 25,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 25
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 25
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba1f"),
                "Name" : "jack25",
                "Age" : 25,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 25
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 25
                    }
                ]
            }
        ],
        "count" : 2
    },
    "25" : {
        "Name" : "jack26",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ef"),
                "Name" : "jack26",
                "Age" : 26,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 26
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 26
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba20"),
                "Name" : "jack26",
                "Age" : 26,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 26
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 26
                    }
                ]
            }
        ],
        "count" : 2
    },
    "26" : {
        "Name" : "jack27",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f0"),
                "Name" : "jack27",
                "Age" : 27,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 27
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 27
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba21"),
                "Name" : "jack27",
                "Age" : 27,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 27
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 27
                    }
                ]
            }
        ],
        "count" : 2
    },
    "27" : {
        "Name" : "jack28",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f1"),
                "Name" : "jack28",
                "Age" : 28,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 28
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 28
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba22"),
                "Name" : "jack28",
                "Age" : 28,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 28
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 28
                    }
                ]
            }
        ],
        "count" : 2
    },
    "28" : {
        "Name" : "jack29",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f2"),
                "Name" : "jack29",
                "Age" : 29,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 29
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 29
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba23"),
                "Name" : "jack29",
                "Age" : 29,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 29
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 29
                    }
                ]
            }
        ],
        "count" : 2
    },
    "29" : {
        "Name" : "jack30",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f3"),
                "Name" : "jack30",
                "Age" : 30,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 30
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 30
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba24"),
                "Name" : "jack30",
                "Age" : 30,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 30
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 30
                    }
                ]
            }
        ],
        "count" : 2
    },
    "30" : {
        "Name" : "jack31",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f4"),
                "Name" : "jack31",
                "Age" : 31,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 31
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 31
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba25"),
                "Name" : "jack31",
                "Age" : 31,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 31
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 31
                    }
                ]
            }
        ],
        "count" : 2
    },
    "31" : {
        "Name" : "jack32",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f5"),
                "Name" : "jack32",
                "Age" : 32,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 32
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 32
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba26"),
                "Name" : "jack32",
                "Age" : 32,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 32
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 32
                    }
                ]
            }
        ],
        "count" : 2
    },
    "32" : {
        "Name" : "jack33",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f6"),
                "Name" : "jack33",
                "Age" : 33,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 33
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 33
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba27"),
                "Name" : "jack33",
                "Age" : 33,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 33
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 33
                    }
                ]
            }
        ],
        "count" : 2
    },
    "33" : {
        "Name" : "jack34",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f7"),
                "Name" : "jack34",
                "Age" : 34,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 34
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 34
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba28"),
                "Name" : "jack34",
                "Age" : 34,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 34
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 34
                    }
                ]
            }
        ],
        "count" : 2
    },
    "34" : {
        "Name" : "jack35",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f8"),
                "Name" : "jack35",
                "Age" : 35,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 35
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 35
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba29"),
                "Name" : "jack35",
                "Age" : 35,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 35
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 35
                    }
                ]
            }
        ],
        "count" : 2
    },
    "35" : {
        "Name" : "jack36",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9f9"),
                "Name" : "jack36",
                "Age" : 36,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 36
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 36
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2a"),
                "Name" : "jack36",
                "Age" : 36,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 36
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 36
                    }
                ]
            }
        ],
        "count" : 2
    },
    "36" : {
        "Name" : "jack37",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9fa"),
                "Name" : "jack37",
                "Age" : 37,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 37
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 37
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2b"),
                "Name" : "jack37",
                "Age" : 37,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 37
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 37
                    }
                ]
            }
        ],
        "count" : 2
    },
    "37" : {
        "Name" : "jack38",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9fb"),
                "Name" : "jack38",
                "Age" : 38,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 38
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 38
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2c"),
                "Name" : "jack38",
                "Age" : 38,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 38
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 38
                    }
                ]
            }
        ],
        "count" : 2
    },
    "38" : {
        "Name" : "jack39",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9fc"),
                "Name" : "jack39",
                "Age" : 39,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 39
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 39
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2d"),
                "Name" : "jack39",
                "Age" : 39,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 39
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 39
                    }
                ]
            }
        ],
        "count" : 2
    },
    "39" : {
        "Name" : "jack40",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9fd"),
                "Name" : "jack40",
                "Age" : 40,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 40
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 40
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2e"),
                "Name" : "jack40",
                "Age" : 40,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 40
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 40
                    }
                ]
            }
        ],
        "count" : 2
    },
    "40" : {
        "Name" : "jack41",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9fe"),
                "Name" : "jack41",
                "Age" : 41,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 41
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 41
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba2f"),
                "Name" : "jack41",
                "Age" : 41,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 41
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 41
                    }
                ]
            }
        ],
        "count" : 2
    },
    "41" : {
        "Name" : "jack42",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2b9ff"),
                "Name" : "jack42",
                "Age" : 42,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 42
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 42
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba30"),
                "Name" : "jack42",
                "Age" : 42,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 42
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 42
                    }
                ]
            }
        ],
        "count" : 2
    },
    "42" : {
        "Name" : "jack43",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba00"),
                "Name" : "jack43",
                "Age" : 43,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 43
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 43
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba31"),
                "Name" : "jack43",
                "Age" : 43,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 43
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 43
                    }
                ]
            }
        ],
        "count" : 2
    },
    "43" : {
        "Name" : "jack44",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba01"),
                "Name" : "jack44",
                "Age" : 44,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 44
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 44
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba32"),
                "Name" : "jack44",
                "Age" : 44,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 44
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 44
                    }
                ]
            }
        ],
        "count" : 2
    },
    "44" : {
        "Name" : "jack45",
        "Person" : [
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba02"),
                "Name" : "jack45",
                "Age" : 45,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 45
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 45
                    }
                ]
            },
            {
                "_id" : ObjectId("54461dce69f872cf5ea2ba33"),
                "Name" : "jack45",
                "Age" : 45,
                "Address" : [
                    "henan",
                    "wuhan"
                ],
                "Course" : [
                    {
                        "Name" : "shuxue",
                        "Score" : 45
                    },
                    {
                        "Name" : "wuli",
                        "Score" : 45
                    }
                ]
            }
        ],
        "count" : 2
    }
}

返回的json

mapReduce

mapReduce其实是一种编程模型,用在分布式计算中,其中有一个“map”函数,一个”reduce“函数。

map:

这个称为映射函数,里面会调用emit(key,value),集合会按照你指定的key进行映射分组。

reduce:

这个称为简化函数,会对map分组后的数据进行分组简化,注意:在reduce(key,value)中的key就是

emit中的key,vlaue为emit分组后的emit(value)的集合,这里也就是很多{"count":1}的数组。

mapReduce:

这个就是最后执行的函数了,参数为map,reduce和一些可选参数。

在MongoDB存储的文档上执行聚合操作非常有用,这种方式的一个限制是聚合函数(比如,SUM、AVG、MIN、MAX)需要通过mapper和reducer函数来定制化实现。

MongoDB没有原生态的用户自定义函数(UDFs)支持。但是它允许使用db.system.js.save命令来创建并保存JavaScript函数,JavaScript函数可以在MapReduce中复用。

第一种统计方式--对应集合直接统计

1.在MongoDB javascript Shell中对Array对象进行了一些扩展,其中新增sum方法,以方便统计数据之用的。

Array.sum=function(arr){
if(arr.length == 0)
return null;
var s = arr[0];
for(var i = 1; i < arr.length; i++)
s += arr[i];
return s;
}

2.例子:按照名称分组,统计每组年龄的和,条件是年龄小于2.

如果统计数量:var map = function(){ emit(this.Name, 1); }   其实是让值永远为1

var map = function(){ emit(this.Name, this.Age); }
var reduce = function( key, values ){ return Array.sum(values); }
var options = {query: { Age: {$lt: 2} }, out: { inline : 1 }}
db.Person.mapReduce(map,reduce,options)

结果如下

/* 0 */
{
    "results" : [
        {
            "_id" : "jack1",
            "value" : 2
        }
    ],
    "timeMillis" : 0,
    "counts" : {
        "input" : 2,
        "emit" : 2,
        "reduce" : 1,
        "output" : 1
    },
    "ok" : 1,
    "_o" : {
        "results" : [
            {
                "_id" : "jack1",
                "value" : 2
            }
        ],
        "timeMillis" : 0,
        "counts" : {
            "input" : 2,
            "emit" : 2,
            "reduce" : 1,
            "output" : 1
        },
        "ok" : 1
    },
    "_keys" : [
        "results",
        "timeMillis",
        "counts",
        "ok"
    ],
    "_db" : {
        "_mongo" : "connection to localhost:27017{ SSL: { sslSupport: false, sslPEMKeyFile: \"\" } }{ SSH: { host: \"\", port: 22, user: \"\", password: \"\", publicKey: { publicKey: \"\", privateKey: \"\", passphrase: \"\" }, currentMethod: 0 } }",
        "_name" : "local",
        "system.indexes" : "local.system.indexes",
        "prototype" : "local.prototype",
        "startup_log" : "local.startup_log",
        "TestCollection" : "local.TestCollection",
        "TestCollection.Person" : "local.TestCollection.Person",
        "DemoTest" : "local.DemoTest",
        "DemoTest.Person" : "local.DemoTest.Person",
        "system" : "local.system",
        "system.js" : "local.system.js",
        "mythings" : "local.mythings",
        "age_totals" : "local.age_totals",
        "Person" : "local.Person"
    }
}

结果json

分析一下:
1. map部分
作用:用于分组的。
emit(param1, param2)
param1:需要分组的字段,this.字段名。
param2:需要进行统计的字段,this.字段名。

2. reduce部分
作用:处理需要统计的字段
var reduce = function(key, values){
......统计字段处理
}
key: 指分组字段(emit的param1)对应的值
values:指需要统计的字段(emit的param2)值组成的数组

简单介绍统计常用的方法:
* 对数值类型进行求和

var reduce = function(key, values){
return Array.sum(values);
}

* 对字符串类型进行拼凑

var reduce = function(key, values){
return values.join(‘, ‘);
}

3. options部分
{ query: { age: {$lt: 25} }, out: "name_totals" }
query:先筛选符合条件的记录出来,再进行分组统计。
out:将分组统计后的结果输出到哪个集合当中。
默认情况下,out所指定的集合在数据库断开连接后再次打开时,依旧存在,并保留之前的所有记录的。

4. 执行分组统计
>db.集合名.mapReduce( map, reduce, options )

第二种统计方式--命令统计

1.命令如下:

注意:out参数 out:"Person_Name" 代表会创建一个临时表Person_Name 然后再从临时表中查找,out:{inline:1} 代表直接显示在当前命令执行的结果中

var map = function(){ emit(this.Name, this.Age); }
var reduce = function( key, values ){ return Array.sum(values); }
db.runCommand({
    mapreduce:"Person",
    map:map,
    reduce:reduce,
    out:"Person_Name",
    keeptemp: false,
    query: { Age:{ $lt: 2 }},
    sort:{ Name:1},    limit:3
    })

解析:
mapreduce:
分组统计的集合名
eg:
mapreduce: ‘mythings‘
不能写成mapreduce: mythings,否则报异常:mythings is not defined

map,reduce :
同上,不做阐述

out :
将分组统计结果输出到某个集合。
注意:不能缺省,必须指定名称,否则报错,报错如下:
“exception: ‘out‘ has to be a string or an object”

keeptemp :
是否保留临时集合(指out指定的集合)
keeptemp:false时会在数据库断开连接后,MongoDB会移除该集合的所有记录。而不是删除。
keeptemp:true时即使数据库断开连接后,再次连接上,该临时集合依旧保持之前所有记录。
keeptemp默认值为true。

query :
筛选记录后,再进行分组统计
eg:
query: { age:{ $lt: 25 }}

sort :
对分组统计的集合进行排序,也即先排序,后再执行分组统计的。
注意:这里的排序需要用到索引,必须先创建索引。

limit :

对分组统计的集合先进行限制返回记录的条数,然后再去进行统计操作。注意:不要理解成对统计后的结果进行限制返回记录条数。

verbose :
显示时间统计信息,取值为true/false

参考资料

http://www.cnblogs.com/shanyou/archive/2012/08/05/2624073.html

http://www.52ij.com/jishu/3925.html

时间: 2024-10-10 05:28:53

Mongodb学习笔记四(Mongodb聚合函数)的相关文章

Mongodb学习笔记一(Mongodb环境配置)

mongodb学习 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0;

MongoDB学习总结(三) —— 常用聚合函数

上一篇介绍了MongoDB增删改查命令的基本用法,这一篇来学习一下MongoDB的一些基本聚合函数. 下面我们直奔主题,用简单的实例依次介绍一下. 1.count()函数 集合的count函数是最简单的聚合函数,返回集合中文档的数量. 2.distinct()函数 用于找出一个集合中,给定键的所有不同的值. 我们还可通过在数据库上运行命令,来执行distinct聚合函数,此时我们必须指定集合和键: 命令参数中,键"distinct"指定统计的集合名称,键"key"指

MongoDB学习笔记(一) MongoDB介绍及安装

这是MongoDB的系列学习笔记的第一篇,主要介绍什么是非关系型数据库MongoDB,如何下载,去哪儿下载,又该怎么正确的安装等一系列问题. 一.前言 最近开始学习非关系型数据库MongoDB,却在博客园上找不到比较系统的教程,很多资料都要去查阅英文网站,效率比较低下.本人不才,借着自学的 机会把心得体会都记录下来,方便感兴趣的童鞋分享讨论.部分资源出自其他博客,旨将零散知识点集中到一起,如果有侵犯您的权利,请联系li- [email protected].大部分内容均系原创,欢迎大家转载分享,

MongoDB学习笔记01 MongoDB的安装及使用

最近由于工作需要,要开始接触mongodb了,于是开始研究mongodb,研究的第一步,自然是从安装及使用开始.日后肯定还会有更多的研究学习,于是准备做下一系列相关笔记,供日后温习. 1.Windows版本安装 1)版本下载 MongoDB下载地址:http://www.mongodb.org/downloads 下载相应版本,此处我下载的是64位. 安装过程很简单,和普通软件安装类似,大部分是Next,安装过程中安装类型选择的customs类型,自定义安装目录为D:/MongoDB OK,直到

MongoDB学习笔记(一) MongoDB介绍及安装(摘)

MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.Mongo的官方网站地址是:http://www.mongodb.org/,读者可以在此获得更详细的信息. 小插曲:什么是NoSql? NoSql,全称是 Not Only Sql,指的是非关系型的数据库.下一代数据库主要解决几个要点:非关系型的.分布式的.开源的.水平可扩展的.原始的目的是为了大规模web应用,这场

Mongodb学习笔记二(Mongodb基本命令)

一.Mongodb命令 说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法. 对于database和collection无需主动创建,在插入数据时,如果database和collection不存在则会自动创建. 常用命令 help命令通过此命令可以看到一些最基本的命令,如图: use命令例如命令[use demodb],创建demodb,不用担心demodb不会创建,当使用use demodb 命令创建第一个collection时会自动创建数据库demodb,如图: 插入数据使用

MongoDB学习笔记&lt;四&gt;

今天继续学习MongoDB的相关知识,主要包括如下: --find详解 --分页与排序 --游标和其他知识 1.指定返回的键 db.person.find({},{"_id":0,"name":1,"country":1})这样就只把name键和country键查询出来了,注意其他的如果不写默认不显示,但是如果_id如果不写,默认是显示的. 2.查询条件 db.person.find({"age":{$gte:25,$lte:

MongoDB学习笔记四:索引

索引就是用来加速查询的.创建数据库索引就像确定如何组织书的索引一样.但是你的优势是知道今后做何种查询,以及哪些内容需要快速查找.比如:所有的查询都包括"date"键,那么很可能(至少)需要建立一个关于"date"的索引.如果要查询用户名,则不必索引"user_num"键,因为根本不会对其进行查询.现在要依照某个键进行查找:> db.people.find({"username" : "mark"})当

MongoDB 学习笔记四 C#调用MongoDB

驱动 下载 https://github.com/mongodb/mongo-csharp-driver/downloads 项目地址: https://github.com/mongodb/mongo-csharp-driver 1.10 使用参考: http://mongodb.github.io/mongo-csharp-driver/1.10/ 1.10 API http://api.mongodb.org/csharp/1.10/ 官方推荐使用Nuget进行安装.但我搜索时出来东西太多