mongodb中的排序和索引快速学习

  在mongodb中,排序和索引其实都是十分容易的,先来小结下排序:


1 先插入些数据
   db.SortTest.insert( { name : "Denis", age : 20 } )
db.SortTest.insert( { name : "Abe", age : 30 } )
db.SortTest.insert( { name : "John", age : 40 } )
db.SortTest.insert( { name : "Xavier", age : 10 } )
db.SortTest.insert( { name : "Zen", age : 50 } )

2 然后默认检索一下
   db.SortTest.find()
{ "_id" : ObjectId("50f6811c03141917bce6459f"), "name" : "Denis", "age" : 20 }
{ "_id" : ObjectId("50f6811d03141917bce645a0"), "name" : "Abe", "age" : 30 }
{ "_id" : ObjectId("50f6811d03141917bce645a1"), "name" : "John", "age" : 40 }
{ "_id" : ObjectId("50f6811d03141917bce645a2"), "name" : "Xavier", "age" : 10 }
{ "_id" : ObjectId("50f6811e03141917bce645a3"), "name" : "Zen", "age" : 50 }
  排序的话,注意1是升序,-1是降序,如下:
  db.SortTest.find().sort({name: -1}) ,则对name字段降序
  { "_id" : ObjectId("50f6811e03141917bce645a3"), "name" : "Zen", "age" : 50 }
{ "_id" : ObjectId("50f6811d03141917bce645a2"), "name" : "Xavier", "age" : 10 }
{ "_id" : ObjectId("50f6811d03141917bce645a1"), "name" : "John", "age" : 40 }
{ "_id" : ObjectId("50f6811c03141917bce6459f"), "name" : "Denis", "age" : 20 }
{ "_id" : ObjectId("50f6811d03141917bce645a0"), "name" : "Abe", "age" : 30 }

  可以同时多个字段的排列,比如:
   db.SortTest.find().sort( { age: -1 , name: 1} );

3 关于索引
  
  首先,mongodb的是B-tree的索引了。要注意的是,一个collection不能超过64个索引,
索引的大小不能超过1024字节,其中包括字段名和值和命名空间。
   首先照样创建数据:
db.Indexing.insert( { name : "Denis", age : 20 } )
db.Indexing.insert( { name : "Abe", age : 30 } )
db.Indexing.insert( { name : "John", age : 40 } )
db.Indexing.insert( { name : "Xavier", age : 10 } )
db.Indexing.insert( { name : "Zen", age : 50 } )

  首先,尝试看下mongodb的执行计划:
db.Indexing.find({name: "Denis"}).explain(),这个是看当查找Denis的执行情况,
结果如下:
  {
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 0,
        "nscanned" : 0,
        "nscannedObjectsAllPlans" : 0,
        "nscannedAllPlans" : 0,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {

        },
        "server" : "Denis:27017"
}
 
下面加个索引,如下:
   db.Indexing.ensureIndex({name: 1});
  其中依然,1是升序,-1是降序,再看下执行计划:

db.Indexing.find({name: "Denis"}).explain()
结果为:

  {
        "cursor" : "BtreeCursor name_1",
        "isMultiKey" : false,
        "n" : 1,
        "nscannedObjects" : 1,
        "nscanned" : 1,
        "nscannedObjectsAllPlans" : 1,
        "nscannedAllPlans" : 1,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 1,
        "indexBounds" : {
                "name" : [
                        [
                                "Denis",
                                "Denis"
                        ]
                ]
        },
        "server" : "Denis:27017"
}
   可以看到,"cursor" 一栏中,已经变成了btree了;并且"indexBounds" :中现在有内容了。
  然后可以删除索引:
  db.Indexing.dropIndex({name: 1});
  删除所有索引
  
4 创建唯一索引
   db.Indexing.ensureIndex({name: 1}, {unique: true});
  也就是加上{unique: true}就可以了

5 创建复合索引
   比如要创建name和age的复合索引,直接如下
db.Indexing.ensureIndex({name: 1, age : 1});
  如果要复合唯一索引,则:
   db.Indexing.ensureIndex({name: 1, age : 1}, {unique: true})
   同样要注意的是,复合索引,要一起用才有效果,比如:

  db.Indexing.find({name: "Denis"}).explain(),只按一个NAME,索引使用情况为:
  {
        "cursor" : "BtreeCursor name_1_age_1",
        "isMultiKey" : false,
        "n" : 1,
        "nscannedObjects" : 1,
        "nscanned" : 1,
        "nscannedObjectsAllPlans" : 1,
        "nscannedAllPlans" : 1,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {
                "name" : [
                        [
                                "Denis",
                                "Denis"
                        ]
                ],
                "age" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        },
        "server" : "Denis:27017"
}
  如果db.Indexing.find({age: "20"}).explain(),则没能使用索引

{
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 5,
        "nscanned" : 5,
        "nscannedObjectsAllPlans" : 5,
        "nscannedAllPlans" : 5,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {

        },
        "server" : "Denis:27017"
}

你可能感兴趣的:(mongodb)