MongoDB系列文章推荐.docx
《MongoDB系列文章推荐.docx》由会员分享,可在线阅读,更多相关《MongoDB系列文章推荐.docx(141页珍藏版)》请在冰点文库上搜索。
MongoDB系列文章推荐
MongoDB系列文章推荐
今天才发现这么一系列的学习笔记,绝对算是一个失误。
下面是一个长达十节的MongoDB系列文章,几乎可以称作简单的中文文档。
非常实用而系统。
∙MongoDB:
1.Database
∙MongoDB:
2.BasicUsage
∙MongoDB:
3.SchemaDesign
∙MongoDB:
4.Index
∙MongoDB:
5.Admin
∙MongoDB:
6.Optimization
∙MongoDB:
7.Replication
(1)
∙MongoDB:
7.Replication
(2)
∙MongoDB:
8.Sharding
(1)
∙MongoDB:
8.Sharding
(2)
∙MongoDB:
9.GridFS
∙MongoDB:
10.MapReduce
MongoDB系列文章推荐
今天才发现这么一系列的学习笔记,绝对算是一个失误。
下面是一个长达十节的MongoDB系列文章,几乎可以称作简单
的中文文档。
非常实用而系统。
*MongoDB:
1.Database
*MongoDB:
2.BasicUsage
*MongoDB:
3.SchemaDesign
*MongoDB:
4.Index
*MongoDB:
5.Admin
*MongoDB:
6.Optimization
*MongoDB:
7.Replication
(1)
*MongoDB:
7.Replication
(2)
*MongoDB:
8.Sharding
(1)
*MongoDB:
8.Sharding
(2)
*MongoDB:
9.GridFS
*MongoDB:
10.MapReduce
MongoDB:
1.Database
mongo是MongoDB自带的交互式Javascriptshell,用来对Mongod进行操作和管理的交互式环境。
使用“./mongo–help”可查看相关连接参数。
$./mongo--help
MongoDBshellversion:
1.5.3
usage:
./mongo[options][dbaddress][filenames(endingin.js)]
dbaddresscanbe:
foofoodatabaseonlocalmachine
192.169.0.5/foofoodatabaseon192.168.0.5machine
192.169.0.5:
9999/foofoodatabaseon192.168.0.5machineonport9999
options:
--shellruntheshellafterexecutingfiles
--nodbdon'tconnecttomongodonstartup-no'dbaddress'
argexpected
--quietbelesschatty
--portargporttoconnectto
--hostargservertoconnectto
--evalargevaluatejavascript
-u[--username]argusernameforauthentication
-p[--password]argpasswordforauthentication
-h[--help]showthisusageinformation
--versionshowversioninformation
--ipv6enableIPv6support(disabledbydefault)
filenames:
alistoffilestorun.fileshavetoendin.jsandwillexitafterunless--shellis
specified
相关命令很多,要习惯使用“help”。
$./mongo
MongoDBshellversion:
1.5.3
connectingto:
test
type"help"forhelp
>help
helpadminmiscshellcommands
showdbsshowdatabasenames
showcollectionsshowcollectionsincurrentdatabase
showusersshowusersincurrentdatabase
showprofileshowmostrecentsystem.profileentrieswithtime>=1ms
usesetcurrentdatabaseto
db.help()helponDBmethods
db.foo.help()helponcollectionmethods
db.foo.find()listobjectsincollectionfoo
db.foo.find({a:
1})listobjectsinfoowherea==1
itresultofthelastlineevaluated;usetofurtheriterate
exitquitthemongoshell
(1)MongoDB会自动创建数据库(db)和集合(collection),无需显式执行。
$./mongo
MongoDBshellversion:
1.5.3
connectingto:
test
type"help"forhelp
>showdbs//查看当前数据库列表
admin
local
>useblog//切换到工作数据库
switchedtodbblog
>db//当前数据库
blog
>for(vari=0;i<10;i++)db.users.save({name:
"user"+i,age:
i})//插入数据
>showdbs//数据库blog被创建
admin
blog
local
>showcollections//列表users被创建
system.indexes
users
>db.copyDatabase("blog","blog2")//复制数据库
{"ok":
true}
>showdbs//数据库blog2被创建
admin
blog
blog2
local
>useblog2//切换到blog2
switchedtodbblog2
>showcollections//查看集合列表
system.indexes
users
>db.users.find()//查看被复制的数据
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981a"),"name":
"user0","age":
0}
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981b"),"name":
"user1","age":
1}
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981c"),"name":
"user2","age":
2}
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981d"),"name":
"user3","age":
3}
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981e"),"name":
"user4","age":
4}
{"_id":
ObjectId("4c33f8fcecf2b9320ac2981f"),"name":
"user5","age":
5}
{"_id":
ObjectId("4c33f8fcecf2b9320ac29820"),"name":
"user6","age":
6}
{"_id":
ObjectId("4c33f8fcecf2b9320ac29821"),"name":
"user7","age":
7}
{"_id":
ObjectId("4c33f8fcecf2b9320ac29822"),"name":
"user8","age":
8}
{"_id":
ObjectId("4c33f8fcecf2b9320ac29823"),"name":
"user9","age":
9}
>db.dropDatabase()//删除数据库blog2
{"dropped":
"blog2","ok":
true}
>showdbs//确认数据库删除成功
admin
blog
local
>useblog//切换回blog
switchedtodbblog
>db.users.drop()//删除集合users
true
>showcollections//确认集合被删除
system.indexes
>exit
bye
(2)还可以在多台服务器之间复制数据库。
server64$./mongo
MongoDBshellversion:
1.5.3
connectingto:
test
type"help"forhelp
>useblog
switchedtodbblog
>for(vari=0;i<10;i++)db.users.save({name:
"user"+i,age:
i})
>usenews
switchedtodbnews
>for(vari=0;i<10;i++)db.articles.save({title:
"title"+i})
>showdbs
admin
blog
local
news
>exit
bye
准备好源数据库后,我们开始在复制。
server32:
$./mongo
MongoDBshellversion:
1.5.4
connectingto:
test
>db.copyDatabase("blog","blog","192.168.1.202")//从源服务器复制blog数据库
{"ok":
true}
>showdbs//复制成功
admin
blog
local
>useblog
switchedtodbblog
>showcollections
system.indexes
users
>db.users.find()
{"_id":
ObjectId("4c33fadb15b7f104d297e644"),"name":
"user0","age":
0}
{"_id":
ObjectId("4c33fadb15b7f104d297e645"),"name":
"user1","age":
1}
{"_id":
ObjectId("4c33fadb15b7f104d297e646"),"name":
"user2","age":
2}
{"_id":
ObjectId("4c33fadb15b7f104d297e647"),"name":
"user3","age":
3}
{"_id":
ObjectId("4c33fadb15b7f104d297e648"),"name":
"user4","age":
4}
{"_id":
ObjectId("4c33fadb15b7f104d297e649"),"name":
"user5","age":
5}
{"_id":
ObjectId("4c33fadb15b7f104d297e64a"),"name":
"user6","age":
6}
{"_id":
ObjectId("4c33fadb15b7f104d297e64b"),"name":
"user7","age":
7}
{"_id":
ObjectId("4c33fadb15b7f104d297e64c"),"name":
"user8","age":
8}
{"_id":
ObjectId("4c33fadb15b7f104d297e64d"),"name":
"user9","age":
9}
>usenews
switchedtodbnews
>db.cloneDatabase("192.168.1.202")//从源服务器克隆当前数据库(news)
{"ok":
true}
>showdbs
admin
blog
local
news
>showcollections
articles
system.indexes
>db.articles.find()
{"_id":
ObjectId("4c33fb6215b7f104d297e64e"),"title":
"title0"}
{"_id":
ObjectId("4c33fb6215b7f104d297e64f"),"title":
"title1"}
{"_id":
ObjectId("4c33fb6215b7f104d297e650"),"title":
"title2"}
{"_id":
ObjectId("4c33fb6215b7f104d297e651"),"title":
"title3"}
{"_id":
ObjectId("4c33fb6215b7f104d297e652"),"title":
"title4"}
{"_id":
ObjectId("4c33fb6215b7f104d297e653"),"title":
"title5"}
{"_id":
ObjectId("4c33fb6215b7f104d297e654"),"title":
"title6"}
{"_id":
ObjectId("4c33fb6215b7f104d297e655"),"title":
"title7"}
{"_id":
ObjectId("4c33fb6215b7f104d297e656"),"title":
"title8"}
{"_id":
ObjectId("4c33fb6215b7f104d297e657"),"title":
"title9"}
>exit
bye
(3)当我们使用use切换到某个数据库时,变量db表示当前数据库。
还可以用getSisterDB()函数获取其他数据库
的引用。
>useadmin
switchedtodbadmin
>db
admin
>blog=db.getSisterDB("blog")
blog
>blog.users.insert({name:
"abc"})
>blog.users.find({name:
"abc"})
{"_id":
ObjectId("4c3419b0492aa4cfbec11895"),"name":
"abc"}
(4)调用fsync命令,可以强制将内存中缓存数据写回数据库文件。
如果不想等待,可添加async参数异步执行。
>useadmin
switchedtodbadmin
>db.runCommand({fsync:
1})
{"numFiles":
6,"ok":
true}
>db.runCommand({fsync:
1,async:
true})
{"numFiles":
6,"ok":
true}
(5)某些时候需要锁定系统,阻塞所有写操作,诸如备份、整理数据库等等。
锁定时读操作不受影响。
$./mongo
MongoDBshellversion:
1.5.3
connectingto:
test
type"help"forhelp
>useblog
switchedtodbblog
>admin=db.getSisterDB("admin")
admin
>admin.runCommand({fsync:
1,lock:
1})//锁定
{
"info":
"nowlockedagainstwrites,usedb.$cmd.sys.unlock.findOne()tounlock",
"ok":
true
}
>db.users.find()//读操作正常
{"_id":
ObjectId("4c33fadb15b7f104d297e644"),"name":
"user0","age":
0}
{"_id":
ObjectId("4c33fadb15b7f104d297e645"),"name":
"user1","age":
1}
{"_id":
ObjectId("4c33fadb15b7f104d297e646"),"name":
"user2","age":
2}
{"_id":
ObjectId("4c33fadb15b7f104d297e647"),"name":
"user3","age":
3}
{"_id":
ObjectId("4c33fadb15b7f104d297e648"),"name":
"user4","age":
4}
{"_id":
ObjectId("4c33fadb15b7f104d297e649"),"name":
"user5","age":
5}
{"_id":
ObjectId("4c33fadb15b7f104d297e64a"),"name":
"user6","age":
6}
{"_id":
ObjectId("4c33fadb15b7f104d297e64b"),"name":
"user7","age":
7}
{"_id":
ObjectId("4c33fadb15b7f104d297e64c"),"name":
"user8","age":
8}
{"_id":
ObjectId("4c33fadb15b7f104d297e64d"),"name":
"user9","age":
9}
>db.users.save({name:
"xyz"})//写操作被阻塞,等待...
另开启一个终端,解除锁定。
>useadmin
switchedtodbadmin
>db.$cmd.sys.unlock.findOne()
{"ok":
1,"info":
"unlockrequested"}
解除后,前一终端被阻塞的写操作正确返回。
(6)调用validate()验证集合是否存在错误。
>db.users.validate()
{
"ns":
"blog.users",
"result":
"
validate
firstExtent:
0:
2600ns:
blog.users
lastExtent:
0:
23d00ns:
blog.users
#extents:
2
datasize?
:
4640nrecords?
:
116lastExtentSize:
9216
padding:
1
firstextent:
loc:
0:
2600xnext:
0:
23d00xprev:
null
nsdiag:
blog.users
size:
2304firstRecord:
0:
26b0lastRecord:
0:
2ec8
116objectsfound,nobj:
116
6496bytesdataw/headers
4640bytesdatawout/headers
deletedList:
0000000010000000000
deleted:
n:
1size:
4672
nIndexes:
1
blog.users.$_id_keys:
116
",
"ok":
true,
"valid":
true,
"lastExtentSize":
9216
}
MongoDB:
2.BasicUsage
须安装PyMongo(Documentation)。
$sudoeasy_install-Upymongo
(注:
以下>提示符表示mongoJS代码,>>>为Python代码)
1.INSERT
使用insert插入文档。
>useblog
switchedtodbblog
>u={name:
"user1",age:
23}
{"name":
"user1","age":
23}
>db.users.insert(u)
>u2=db.users.findOne({name:
"user1"})
{
"_id":
ObjectId("4c44fe0edef8f3492fe67d60"),
"name":
"user1",
"age":
23
}
>u2.age+=3
26
>db.users.save(u2)
>db.users.find()
{"_id":
ObjectId("4c44fe0edef8f3492fe67d60"),"name":
"user1","age":
26}
save()可插入新文档,也可以更新(update)一个已有的文档。
下面是用PyMongo写的相同的代码。
>>>importpymongo
>>>conn=pymongo.Connection(host="192.168.1.202")
>>>db=conn.blog
>>>u={"name":
"user1","age":
23}
>>>db.users.save(u)
ObjectId('4c456e0a499