您的位置:首页 > 数据库 > Mongodb

MongoDB基本介绍及一些用法

2012-11-12 23:26 573 查看
满足海量存储需求和访问的面向文档的数据库:MongoDB,CouchDB

  MongoDB

  Nice, I like it very much.
  面向文档的非关系数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能。MongoDB是用C++开发的,而CouchDB则是Erlang开发的:
  1、MongoDB
  MongoDB是 一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似
json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几 乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
  Mongo主要解决的是海量数据的访问效率问题,根据官方的文档,当数据量达到50GB以上的时候,Mongo的数据库访问速度是MySQL的 10倍以上。Mongo的并发读写效率不是特别出色,根据官方提供的性能测试表明,大约每秒可以处理0.5万-1.5次读写请求。对于Mongo的并发读 写性能,我(robbin)也打算有空的时候好好测试一下。
  因为Mongo主要是支持海量数据存储的,所以Mongo还自带了一个出色的分布式文件系统GridFS,可以支持海量的数据存储,但我也看到有些评论认为GridFS性能不佳,这一点还是有待亲自做点测试来验证了。
  最后由于Mongo可以支持复杂的数据结构,而且带有强大的数据查询功能,因此非常受到欢迎,很多项目都考虑用MongoDB来替代MySQL来实现不是特别复杂的Web应用,比方说whywe
migrated from MySQL toMongoDB就是一个真实的从MySQL迁移到MongoDB的案例,由于数据量实在太大,所以迁移到了Mongo上面,数据查询的速度得到了非常显著的提升。
  MongoDB也有一个ruby的项目MongoMapper,是模仿Merb的DataMapper编写的MongoDB的接口,使用起来非常简单,几乎和DataMapper一模一样,功能非常强大易用。
  MongoDB语法:
  启动服务

view source

print?

1
mongod.exe –dbpath F:\DataBase\MongoDB\db\
  –dbpath 数据文件存放路径

  –port 数据服务端口

  启动客户端

view source

print?

1
mongo.exe cclove
  cclove 所连接的数据库名称
  数据库操作语法

view source

print?

01
mongo –path
02
db.AddUser(username,password) 添加用户
03
db.auth(usrename,password)    设置数据库连接验证
04
db.cloneDataBase(fromhost)    从目标服务器克隆一个数据库
05
db.commandHelp(name)          returns the
help
for
the command
06
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
07
db.createCollection(name,{size:
3333
,capped:
333
,
max
:
88888
})
创建一个数据集,相当于一个表
08
db.currentOp()                取消当前库的当前操作
09
db.dropDataBase()             删除当前数据库
10
db.
eval
(func,args)            run code server
-
side
11
db.getCollection(cname)       取得一个数据集合,同用法:db[
'cname'
]
or
db.cname
12
db.getCollenctionNames()      取得所有数据集合的名称列表
13
db.getLastError()             返回最后一个错误的提示消息
14
db.getLastErrorObj()          返回最后一个错误的对象
15
db.getMongo()                 取得当前服务器的连接对象get the server connection
object
16
db.getMondo().setSlaveOk()    allow this connection to read
from
then nonmaster membr of a replica pair
17
db.getName()                  返回当操作数据库的名称
18
db.getPrevError()             返回上一个错误对象
19
db.getProfilingLevel()        ?什么等级
20
db.getReplicationInfo()       ?什么信息
21
db.getSisterDB(name)          get the db at the same server as this onew
22
db.killOp()                   停止(杀死)在当前库的当前操作
23
db.printCollectionStats()     返回当前库的数据集状态
24
db.printReplicationInfo()
25
db.printSlaveReplicationInfo()
26
db.printShardingStatus()      返回当前数据库是否为共享数据库
27
db.removeUser(username)       删除用户
28
db.repairDatabase()           修复当前数据库
29
db.resetError()
30
db.runCommand(cmdObj)         run a database command.
if
cmdObj
is
a
string, turns it into {cmdObj:
1
}
31
db.setProfilingLevel(level)
0
=
off,
1
=
slow,
2
=
all
32
db.shutdownServer()           关闭当前服务程序
33
db.version()                  返回当前程序的版本信息
  数据集(表)操作语法

view source

print?

01
db.linlin.find({
id
:
10
})         返回linlin数据集
ID
=
10
的数据集
02
db.linlin.find({
id
:
10
}).count() 返回linlin数据集
ID
=
10
的数据总数
03
db.linlin.find({
id
:
10
}).limit(
2
)返回linlin数据集
ID
=
10
的数据集从第二条开始的数据集
04
db.linlin.find({
id
:
10
}).skip(
8
)
返回linlin数据集
ID
=
10
的数据集从
0
到第八条的数据集
05
db.linlin.find({
id
:
10
}).limit(
2
).skip(
8
)
返回linlin数据集
ID
=
1
=
的数据集从第二条到第八条的数据
06
db.linlin.find({
id
:
10
}).sort()  返回linlin数据集
ID
=
10
的排序数据集
07
db.linlin.findOne([query])      返回符合条件的一条数据
08
db.linlin.getDB()               返回此数据集所属的数据库名称
09
db.linlin.getIndexes()          返回些数据集的索引信息
10
db.linlin.group({key:…,initial:…,
reduce
:…[,cond:...]})
11
db.linlin.mapReduce(mayFunction,reduceFunction,<optional params>)
12
db.linlin.remove(query)                     在数据集中删除一条数据
13
db.linlin.renameCollection(newName)         重命名些数据集名称
14
db.linlin.save(obj)                         往数据集中插入一条数据
15
db.linlin.stats()                           返回此数据集的状态
16
db.linlin.storageSize()                     返回此数据集的存储大小
17
db.linlin.totalIndexSize()                  返回此数据集的索引文件大小
18
db.linlin.totalSize()                       返回些数据集的总大小
19
db.linlin.update(query,
object
[,upsert_bool])在此数据集中更新一条数据
20
db.linlin.validate()                        验证此数据集
21
db.linlin.getShardVersion()                 返回数据集共享版本号
22
db.linlin.find({‘name’:'foobar’})   select
*
from
linlin wherename
=
’foobar’
23
db.linlin.find()                    select
*
from
linlin
24
db.linlin.find({‘
ID
’:
10
}).count()   select
count(
*
)
from
linlin where
ID
=
10
25
db.linlin.find().skip(
10
).limit(
20
) 从查询结果的第十条开始读
20
条数据
select
*
from
linlin limit
10
,
20
———
-
mysql
26
db.linlin.find({‘
ID
’:{$
in
:[
25
,
35
,
45
]}})
select
*
from
linlin where
ID
in
(
25
,
35
,
45
)
27
db.linlin.find().sort({‘
ID
’:
-
1
})
select
*
from
linlin order by
ID
desc
28
db.linlin.distinct(‘name’,{‘
ID
’:{$lt:
20
}})
selectdistinct(name)
from
linlin where
ID
<
20
29
db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},
reduce
:function(obj,prev){prev.msum
+
=
obj.marks;},initial:{msum:
0
}})
30
select name,
sum
(marks)
from
linlin group
by name
31
db.linlin.find(‘this.
ID
<
20
′,{name:
1
})
selectname 
from
linlin where
ID
<
20
32
db.linlin.insert({‘name’:
'foobar’,'
age’:
25
})
insert into linlin (‘name’,'age’)values(‘foobar’,
25
)
33
db.linlin.insert({‘name’:
'foobar’,'
age’:
25
,’email’:'cclove2@
163.com
’})
34
db.linlin.remove({})                  delete 
*
from
linlin
35
db.linlin.remove({‘age’:
20
})          delete linlin whereage
=
20
36
db.linlin.remove({‘age’:{$lt:
20
}})    delete linlin whereage<
20
37
db.linlin.remove({‘age’:{$lte:
20
}})   delete linlin whereage<
=
20
38
db.linlin.remove({‘age’:{$gt:
20
}})    delete linlin whereage>
20
39
db.linlin.remove({‘age’:{$gte:
20
}})   delete linlin whereage>
=
20
40
db.linlin.remove({‘age’:{$ne:
20
}})    delete linlin whereage!
=
20
41
db.linlin.update({‘name’:'foobar’},{$
set
:{‘age’:
36
}})
update linlin
set
age
=
36
where name
=
’foobar’
42
db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:
3
}})  update linlin 
set
age
=
age
+
3
where
name
=
’foobar’
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: