游标
> for(i=0;i<100;i++){
... db.c.insert({x:i})
... }
WriteResult({ "nInserted" : 1 })
var cursor=db.c.find();
> while(cursor.has
cursor.hasNext( cursor.hasOwnProperty(
> while(cursor.hasNext()){
... obj=cursor.next();
... print(obj.x);
... }
> db.c.find().limit(3)
{ "_id" : ObjectId("56a226a66efa22a91f284361"), "x" : 0 }
{ "_id" : ObjectId("56a226a66efa22a91f284362"), "x" : 1 }
{ "_id" : ObjectId("56a226a66efa22a91f284363"), "x" : 2 }
> var cursor=db.c.find().skip(10).limit(5)
> var cursor=db.c.find().skip(10).limit(5)
> while(cursor.hasNext()){
... ... obj=cursor.next();
... ... print(obj.x);
... }
10
11
12
13
14
> var cursor=db.c.find().skip(10).limit(6).sort({"x":1})
> var cursor=db.c.find().skip(10).limit(6).sort({"x":1})1是升序-1降序
> while(cursor.hasNext()){
... ... ... obj=cursor.next();
... ... ... print(obj.x);
}
10
11
12
13
14
15
用skip实现分页
var page1=db.find().sort({"date":-1}).limit(100)
var latest=null;
while(page1.hasNext()){
latest=page1.next();
display(latest);
}
//get next page
var page2=db.foo.find({"date":{"$gt":latest.date}});
page2.sort({"date":-1}).limit(100)
mongodb索引
可以任意列建索引
索引构造和使用与传统关系型数据库几乎一样
使用索引可以加快查询,但会降低修改,插入等的性能
内嵌文档也可以建立索引
db.people.insert([
{"username":"hello","age":23,"user_id":0},
{"username":"hello","age":23,"user_id":1},
{"username":"hello","age":23,"user_id":2},
{"username":"hello","age":23,"user_id":3},
{"username":"hello","age":23,"user_id":4},
{"username":"hello","age":23,"user_id":5},
{"username":"hello","age":23,"user_id":6},
{"username":"hello","age":23,"user_id":7},
{"username":"hello","age":23,"user_id":8},
{"username":"hello","age":23,"user_id":9},
{"username":"hello","age":23,"user_id":10}
])
建立索引
db.people.ensureIndex({"username":1})
db.people.ensureIndex({"username":1,"age":-1})
explain查看
> db.people.find().explain();
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 11,
"nscannedObjects" : 11,
"nscanned" : 11,
"nscannedObjectsAllPlans" : 11,
"nscannedAllPlans" : 11,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"server" : "localhost.localdomain:27017",
"filterSet" : false
}
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "db.abc" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "db.c" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "db.people" }
{ "v" : 1, "key" : { "username" : 1 }, "name" : "username_1", "ns" : "db.people" }
{ "v" : 1, "key" : { "username" : 1, "age" : -1 }, "name" : "username_1_age_-1", "ns" : "db.people" }
> db.runCommand({"dropIndexes":"people","index":"*"});
{
"nIndexesWas" : 3,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
聚合:count
db.people.count()
> db.runCommand({"distinct":"people","key":"age"})
{
"values" : [
23
],
"stats" : {
"n" : 11,
"nscanned" : 11,
"nscannedObjects" : 11,
"timems" : 0,
"cursor" : "BasicCursor"
},
"ok" : 1
}
聚合操作
db.people.group(
{
"key":{"username":true},
"initial":{"csum":0},
"reduce":function(obj,prev){
prev.sum+=obj.age
}
})
db.runCommand({"group":{
"ns":"people",
"key":"username",
"initial":{"csum":0},
"$reduce":function(obj,prev){
prev.sum+=obj.age
}
}})
db.runCommand({"group":{
... "ns":"people",
... "key":"username",
... "initial":{"csum":0},
... "$reduce":function(obj,prev){
... prev.sum+=obj.age
... }
...
... }})
{
"retval" : [
{
"csum" : 0,
"sum" : NaN
}
],
"count" : 11,
"keys" : 1,
"ok" : 1
}
寻找集合中所有的键
map=function(){
for(var key in this){
emit(key,{count:1})
}}
reduce=function(key,emits){
total=0;
for(var i in emits){
total+=emits[i].count;
}
return {"count":total};
}
db.runCommand({"mapreduce":"people","map":map,"reduce":reduce,"out":"result"});
{
"result" : "result",
"timeMillis" : 57,
"counts" : {
"input" : 11,
"emit" : 44,
"reduce" : 4,
"output" : 4
},
"ok" : 1
}