美文网首页
MongoDB 索引

MongoDB 索引

作者: 寻梦的尕柳 | 来源:发表于2017-08-01 11:57 被阅读24次
    # 命令行远程连接到 mongodb
    mongo --host 127.0.0.1 --port 27017 -u test -p root --authenticationDatabase test
    
    # 切换数据库
    use test
    
    # 查询 collection 列表
    show collections
    
    # 查询 user 的所有索引
    db.user.getIndexes()
    
    # 建立单个字段的索引
    # 数字1表示索引按升序存储,-1表示索引按照降序方式存储
    db.user.ensureIndex({"username":1})
    
    # 建立多个字段的联合索引
    db.user.ensureIndex({"username":1, "age":-1})
    
    # 删除索引
    db.user.dropIndex({"username":1})
    

    相关文章

      网友评论

          本文标题:MongoDB 索引

          本文链接:https://www.haomeiwen.com/subject/dkljlxtx.html