mongoDB基本使用(二)(二)

2015-11-21 01:33:31 · 作者: · 浏览: 7
1代表正向排序,-1代表反向排序
?
db.jerome_collection.ensureIndex({x:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
db.jerome_collection.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "jerome.jerome_collection"
    },
    {
        "v" : 1,
        "key" : {
            "x" : 1
        },
        "name" : "x_1",
        "ns" : "jerome.jerome_collection"
    }
]

?

?
(使用数据库之前创建索引更好)
?
索引虽然会使写入的数度变慢,但是查询的速度变快了。