MongoDB学习

来源:互联网 发布:c语言猜拳游戏代码 编辑:程序博客网 时间:2024/04/27 17:19
数据库分类:
1.Sql数据库:支持Sql语言的数据库
    oracle mysql
2.NoSql数据库:不支持Sql语言的数据库
    Redis,MongoDB....     NoSql ----Not only Sql (悄悄的不支持)

sql数据库                NoSql数据库
实时一致性              简单便捷
事务                        方便扩展
多表联合查询          更好的性能
为什么是MongoDB
1.无数据结构限制
    1.没有表结构的概念,每条记录可以有完全不同的结构
    2.业务开发方便快捷
    3.sql数据库需要事先定义表结构再使用
{name:"小明",sex:"男"}
{name:"小红",address:"上海"}
{name:"小兰",home:[{"山东"},{"江西"}]}
2.完全的索引支持
    1.redis的key-value
    2.hbase的单索引,二级索引需要自己的实现
单键索引,多键索引:{x:1,y:1}
数组索引:["apple","lemon"]
全文索引:"i am a bird"(中文还不支持)
地理位置索引:2D
3.方便的冗余 和扩展
    1.复制集保证数据安全
    2.分片扩展数据规模
MongoDB环境:64位Linux
MongoDB版本:2.6.5
ssh根据:xShell
文本编辑器:vim与notepad++
4.搭建简单的MongoDB服务器
    1.首先,创建一个叫=叫做mongoDB的目录,进入到目录中
    2.创建文件夹:data,用来存储数据库的数据文件。
    3.创建文件夹:log,用来存储数据库的日志文件。
    4.创建文件夹:bin,用来存储数据库的可执行文件。
    5.创建文件夹:conf,用来存储数据库的配置文件。



Microsoft Windows [版版本本 6.3.9600]
(c) 2013 Microsoft Corporation。。保保留留所所有有权权利利。。

C:\Users\Sunshine>D:

D:\>cd MongoDB

D:\MongoDB>cd bin

D:\MongoDB\bin>mongo 127.0.0.1:27017
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27017/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> show dbs
local   0.078125GB
> show dbs
local   0.078125GB
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries wit
h time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memor
y, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to f
urther iterate
        DBQuery.shellBatchSize = x   set default number of items to display on s
hell
        exit                         quit the mongo shell
> show users
> show logs
global
> show dbs
local   0.078125GB
test    (empty)
> use test
switched to db test
> use local
switched to db local
> db.test_collection.insert({x:1})
> show dbs
local   0.078125GB
test    (empty)
> use test
switched to db test
> db.test_collection.insert({x:1})
>
> show dbs
local   0.078125GB
test    0.203125GB
> shwo collections
Sat Jun 06 10:34:54.149 JavaScript execution failed: SyntaxError: Unexpected ide
ntifier
> use test
switched to db test
> db.test_collections.insert({x:1,y:1})
> show dbs
local   0.078125GB
test    0.203125GB
> show collections
system.indexes
test_collection
test_collections
> show collections
system.indexes
test_collection
test_collections
> db.test_collection.find()
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 1 }
> db.test_collection.insert({x:2,_id:1})
> db.test_collection.insert({x:3,_id:1})
E11000 duplicate key error index: test.test_collection.$_id_  dup key: { : 1.0 }

> db.test_collection.find()
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 1 }
{ "_id" : 1, "x" : 2 }
> for(i=3;i<100;i++)db.test_collection.insert({x:i})
> db.test_collection.find()
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 1 }
{ "_id" : 1, "x" : 2 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4c"), "x" : 3 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4d"), "x" : 4 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4e"), "x" : 5 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4f"), "x" : 6 }
{ "_id" : ObjectId("55725e21e9d45a6566363c50"), "x" : 7 }
{ "_id" : ObjectId("55725e21e9d45a6566363c51"), "x" : 8 }
{ "_id" : ObjectId("55725e21e9d45a6566363c52"), "x" : 9 }
{ "_id" : ObjectId("55725e21e9d45a6566363c53"), "x" : 10 }
{ "_id" : ObjectId("55725e21e9d45a6566363c54"), "x" : 11 }
{ "_id" : ObjectId("55725e21e9d45a6566363c55"), "x" : 12 }
{ "_id" : ObjectId("55725e21e9d45a6566363c56"), "x" : 13 }
{ "_id" : ObjectId("55725e21e9d45a6566363c57"), "x" : 14 }
{ "_id" : ObjectId("55725e21e9d45a6566363c58"), "x" : 15 }
{ "_id" : ObjectId("55725e21e9d45a6566363c59"), "x" : 16 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5a"), "x" : 17 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5b"), "x" : 18 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5c"), "x" : 19 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5d"), "x" : 20 }
Type "it" for more
> it
{ "_id" : ObjectId("55725e21e9d45a6566363c5e"), "x" : 21 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5f"), "x" : 22 }
{ "_id" : ObjectId("55725e21e9d45a6566363c60"), "x" : 23 }
{ "_id" : ObjectId("55725e21e9d45a6566363c61"), "x" : 24 }
{ "_id" : ObjectId("55725e21e9d45a6566363c62"), "x" : 25 }
{ "_id" : ObjectId("55725e21e9d45a6566363c63"), "x" : 26 }
{ "_id" : ObjectId("55725e21e9d45a6566363c64"), "x" : 27 }
{ "_id" : ObjectId("55725e21e9d45a6566363c65"), "x" : 28 }
{ "_id" : ObjectId("55725e21e9d45a6566363c66"), "x" : 29 }
{ "_id" : ObjectId("55725e21e9d45a6566363c67"), "x" : 30 }
{ "_id" : ObjectId("55725e21e9d45a6566363c68"), "x" : 31 }
{ "_id" : ObjectId("55725e21e9d45a6566363c69"), "x" : 32 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6a"), "x" : 33 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6b"), "x" : 34 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6c"), "x" : 35 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6d"), "x" : 36 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6e"), "x" : 37 }
{ "_id" : ObjectId("55725e21e9d45a6566363c6f"), "x" : 38 }
{ "_id" : ObjectId("55725e21e9d45a6566363c70"), "x" : 39 }
{ "_id" : ObjectId("55725e21e9d45a6566363c71"), "x" : 40 }
Type "it" for more
> db.test_collection.find().count()
99
> db.test_collection.find().skip(3).limit(2).sort({x:1})
{ "_id" : ObjectId("55725e21e9d45a6566363c4d"), "x" : 4 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4e"), "x" : 5 }
> db.test_collection.find({x:1})
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 1 }
> db.test_collection.update({x:1},{x:999})
> db.test_collection.find({x:1})
> db.test_collection.find({x:999})
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 999 }
> db.test_collection.insert({x:100,y:100,z:100})
> db.test_collection.find()
{ "_id" : ObjectId("55725c13e9d45a6566363c4a"), "x" : 999 }
{ "_id" : 1, "x" : 2 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4c"), "x" : 3 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4d"), "x" : 4 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4e"), "x" : 5 }
{ "_id" : ObjectId("55725e21e9d45a6566363c4f"), "x" : 6 }
{ "_id" : ObjectId("55725e21e9d45a6566363c50"), "x" : 7 }
{ "_id" : ObjectId("55725e21e9d45a6566363c51"), "x" : 8 }
{ "_id" : ObjectId("55725e21e9d45a6566363c52"), "x" : 9 }
{ "_id" : ObjectId("55725e21e9d45a6566363c53"), "x" : 10 }
{ "_id" : ObjectId("55725e21e9d45a6566363c54"), "x" : 11 }
{ "_id" : ObjectId("55725e21e9d45a6566363c55"), "x" : 12 }
{ "_id" : ObjectId("55725e21e9d45a6566363c56"), "x" : 13 }
{ "_id" : ObjectId("55725e21e9d45a6566363c57"), "x" : 14 }
{ "_id" : ObjectId("55725e21e9d45a6566363c58"), "x" : 15 }
{ "_id" : ObjectId("55725e21e9d45a6566363c59"), "x" : 16 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5a"), "x" : 17 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5b"), "x" : 18 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5c"), "x" : 19 }
{ "_id" : ObjectId("55725e21e9d45a6566363c5d"), "x" : 20 }
Type "it" for more
> db.test_collection.find(x:100)
Sat Jun 06 10:50:03.821 JavaScript execution failed: SyntaxError: Unexpected tok
en :
> db.test_collectsion.update({z:100},{y:99})
> db.test_collection.find({x:100})
{ "_id" : ObjectId("55725fc3e9d45a6566363cad"), "x" : 100, "y" : 100, "z" : 100
}
> db.test_collectsion.update({z:100},{$set:{y:99}})
> db.test_collection.find({x:100})
{ "_id" : ObjectId("55725fc3e9d45a6566363cad"), "x" : 100, "y" : 100, "z" : 100
}
> db.test_collection.find({z:100})
{ "_id" : ObjectId("55725fc3e9d45a6566363cad"), "x" : 100, "y" : 100, "z" : 100
}
>











MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测ofly 发表于 2012-06-21, 9:06 PM. 发表在: 技术交流

->use Admin         (切换到创建用户)

->db.TestDb          (创建数据库) 

->db.addUser(“userName”,”Pwd”)    创建用户

->db.auth(“userName”,”Pwd”)       设置用户为允许连接的用户

->db.createCollection(“TableName”)                                     创建表

->show collections                          查看表是否创建成功

->db.TableName.Save({age:1})                 添加数据

->db.TableName.find()                        查看添加的数据是否成功(如果没有查询到任何的结果,说明添加失败)

 ->添加数据,如果返回的(shell):1 说明有错误

内容简介

1.索引的种类与使用

2.索引的匹配规则

3.如何建立合适的索引

4.索引建立的状况评估


索引的种类

1._id索引

2.单键索引

3.多键索引

4.复合索引

5.过期索引

6.全文索引

7.地理位置索引


_id索引

._id索引是绝大多数集合默认建立的索引

.对于每个插入的数据,MongoDB都会自动生成一条唯一的_id字段

show dbs //显示数据库

use ymg //使用数据库

show tables //显示表

db.tableName.insert({})//大括号里面的是json格式的数据

db.tableName.getIndexes() //获得索引

db.tableName.findOne()

db.tableName.find()

单键索引

1.单键索引是最普通的索引

2.与_id索引不同,单键索引不会自动创建

   例如:一条记录,形式为:{x:1,y:2,z:3}

   db.tableName.ensureIndex({x:1})

   db.tableName.find()

   db.tableName.find({x:1})

多键索引

.多键索引与单键索引创建形式相同,区别在于字段的值

  .单键索引:值为一个单一的值,例如字符串,数字或者日期

  .多键索引:值具有多个记录,例如数组

  use tableName

  db.tableName.getIndexes()

  db.tableName.find()

  db.tableName.insert({x:[1,2,3,4,5]})

  创建了一个多键索引

复合索引

当我们的查询条件不只有一个时,就需要建立复合索引

插入{x:1,y:2,z:3}--->按照x与y的值建立复合索引---->创建索引db.tableName.ensureIndex({x:1,y:1})-->使用{x:1,y:1}作为条件进行查询

过期索引

1.过期索引:是在一段时间后会过期的索引。

2.在索引过期后,相应的数据会被删除。

3.这适合存储一些在一段时间之后会失效的数据比如用户的登录信息,存储的日志。

4.建立方法:

 db.collection.ensureIndex({time:1},{expireAfterSeconds:10})

 db.tableName.ensureIndex({time:1},{expireAfterSeconds:30})

 db.tableName.insert({time:new Date()})

 db.tableName.find()

过期索引的限制

1.存储在过期索引字段的值必须是制定的时间类型

  说明:必须是ISODate或者ISODate数组,不能使用时间戳,否则不能被自动删除

2.如果指定了ISODate数组,则按照最小的时间进行删除

3.过期索引不能是复合索引

4.删除时间不是精确

  说明:删除过程是由后台程序每60s跑一次,而且删除也需要一些时间,索引存在误差

全文索引:

    对字符串与字符串数组创建全文可搜索的索引

使用情况:{author:"",title:"",article:""}

建立方法:

db.articles.ensureIndex({key:"text"})

db.articles.ensureIndex({key_1:"text",key_2:"text"})

db.articles.ensureIndex({"$**":"text"})

如何创建全文索引

 db.tableName.ensureIndex("articel":"text")

如何使用全文索引进行查询

  先插入数据

  db.ymg.insert("article":"aa bb cc dd ee")

  db.ymg.insert("article":"aa bb rr gg")

  db.ymg.insert("article":"aa bb cc hh dsadass")

  查询

 db.articles.find({$text:{$search:"coffee"}})

 db.articles.find({$text:{$search:"aa bb cc"}})

 db.articles.find({$text:{$search:"aa bb -cc"}})  //包含aa bb 不包含cc

 db.articles.find({$text:{$search:"\"aa\"\" bb\" \"cc\""}})  //既包含aa 又包含bb 又包含cc 里面用引号,斜杠+引号是为了防止歧义

 db.articles.find({$text:{$search:"\"aa\"bb cc"}})

全文索引相似度:

$meta操作符:{score:{$meta:"textScore"}}

写在查询条件后面可以返回返回结果的相似度

与sort一起使用,可以达到很好的实用效果

db.tableName.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})

db.tableName.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"})

全文索引的使用限制

全文索引非常强大,但是同样存在限制

每次查询,只能指定一个$text查询

$text查询不能出现在$nor查询中

查询中如果包含了$text,hint不再起作用

很可惜,MongoDB全文索引还不支持中文

索引属性

比较重要的属性

名字

唯一性

稀疏性

是否定时删除(过期索引)

名字,name指定

 db.collection.ensureIndex({},{name:""})

唯一性,unique指定:

 db.collection.ensureIndex({},{unique:true/false})

稀疏性,sparse指定

 db.collection.ensureIndex({},{sparse:true/false})

是否定时删除,expiredAfterSecond指定,TTL,过期索引

地理位置索引

概念:将一些点的位置存储在MongoDB中,创建索引后可以按照位置来查找其他点

子分类:

2d索引,用于存储和查找平面上的点

2dsphere索引,用于存储和查找球面上的点  球面地理位置索引

查找方式:

1.查找距离上某个点一定距离内的点

2.查找包含在某区域内的点

2D索引

创建方式

db.collection.ensureIndex({w:"2d"})

位置表示方式:经纬度【经度、纬度】

取值范围:经度[-180,180] 维度[-90,90]

查询方式:

(1)$near查询:查询距离某个点最近的点

(2$geoWidthin查询:查询某个形状内的点

形状的表示

1.$box:矩形,使用

  {$box:[[<x1><y1>],[<x2><y2>]]}表示

geoNear查询

geoNear使用runCommand命令进行使用,常用使用如下

db.runCommand(

 {geoNear:<collection>,

  near:[x,y],

  minDistance:(对2d索引无效)

  maxDistance:

  num:

  ...}

)

















0 0