第8天筆記 mongdb的操作

1、基本操作

db.AddUser(username,password) 添加用戶

db.auth(usrename,password) 設置數(shù)據(jù)庫連接驗證

db.cloneDataBase(fromhost) 從目標服務器克隆一個數(shù)據(jù)庫

db.commandHelp(name) returns the help for the command

db.copyDatabase(fromdb,todb,fromhost) 復制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務器地址

db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個數(shù)據(jù)集,相當于一個表

db.currentOp() 取消當前庫的當前操作

db.dropDataBase() 刪除當前數(shù)據(jù)庫

db.eval_r(func,args) run code server-side

db.getCollection(cname) 取得一個數(shù)據(jù)集合,同用法:db['cname'] or

db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表

db.getLastError() 返回最后一個錯誤的提示消息

db.getLastErrorObj() 返回最后一個錯誤的對象

db.getMongo() 取得當前服務器的連接對象get the server

db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair

db.getName() 返回當操作數(shù)據(jù)庫的名稱

db.getPrevError() 返回上一個錯誤對象

db.getProfilingLevel()

db.getReplicationInfo() 獲得重復的數(shù)據(jù)

db.getSisterDB(name) get the db at the same server as this onew

db.killOp() 停止(殺死)在當前庫的當前操作

db.printCollectionStats() 返回當前庫的數(shù)據(jù)集狀態(tài)

db.printReplicationInfo()

db.printSlaveReplicationInfo()

db.printShardingStatus() 返回當前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫

db.removeUser(username) 刪除用戶

db.repairDatabase() 修復當前數(shù)據(jù)庫

db.resetError()

db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}

db.setProfilingLevel(level) 0=off,1=slow,2=all

db.shutdownServer() 關閉當前服務程序

db.version() 返回當前程序的版本信息

2、數(shù)據(jù)集(表)操作

db.test.find({id:10}) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集

db.test.find({id:10}).count() 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)

db.test.find({id:10}).limit(2) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集

db.test.find({id:10}).skip(8) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集

db.test.find({id:10}).limit(2).skip(8) 返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)

db.test.find({id:10}).sort() 返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集

db.test.findOne([query]) 返回符合條件的一條數(shù)據(jù)

db.test.getDB() 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱

db.test.getIndexes() 返回些數(shù)據(jù)集的索引信息

db.test.group({key:...,initial:...,reduce:...[,cond:...]})

db.test.mapReduce(mayFunction,reduceFunction,)

db.test.remove(query) 在數(shù)據(jù)集中刪除一條數(shù)據(jù)

db.test.renameCollection(newName) 重命名些數(shù)據(jù)集名稱

db.test.save(obj) 往數(shù)據(jù)集中插入一條數(shù)據(jù)

db.test.stats() 返回此數(shù)據(jù)集的狀態(tài)

db.test.storageSize() 返回此數(shù)據(jù)集的存儲大小

db.test.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小

db.test.totalSize() 返回些數(shù)據(jù)集的總大小

db.test.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)

db.test.validate() 驗證此數(shù)據(jù)集

db.test.getShardVersion() 返回數(shù)據(jù)集共享版本號

3、MongoDB語法與現(xiàn)有關系型數(shù)據(jù)庫SQL語法比較

MongoDB語法 MySql語法

db.test.find({'name':'foobar'}) <==> select * from test where name='foobar'

db.test.find() <==> select * from test

db.test.find({'ID':10}).count() <==> select count(*) from test where ID=10

db.test.find().skip(10).limit(20) <==> select * from test limit 10,20

db.test.find({'ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)

db.test.find().sort({'ID':-1}) <==> select * from test order by ID desc

db.test.distinct('name',{'ID':{$lt:20}}) <==> select distinct(name) from test where ID<20

db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name

db.test.find('this.ID<20',{name:1}) <==> select name from test where ID<20

db.test.insert({'name':'foobar','age':25})<==>insert into test ('name','age') values('foobar',25)

db.test.remove({}) <==> delete * from test

db.test.remove({'age':20}) <==> delete test where age=20

db.test.remove({'age':{$lt:20}}) <==> elete test where age<20

db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20

db.test.remove({'age':{$gt:20}}) <==> delete test where age>20

db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20

db.test.remove({'age':{$ne:20}}) <==> delete test where age!=20

db.test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}}) <==> update test set age=age+3 where

name='foobar'

4、MongoDB主從復制介紹

MongoDB的主從復制其實很簡單,就是在運行 主的服務器 上開啟mongod進程 時,加入?yún)?shù)--master即可,在運行從的服務 器上開啟mongod進程時,加入--slave 和 --source 指定主即可,這樣,在主數(shù)據(jù) 庫更新時,數(shù)據(jù)被復制到從數(shù)據(jù)庫 中

(這里日志 文件 和訪問 數(shù)據(jù)時授權(quán)用戶暫時不考慮 )

下面我在單臺服務器上開啟2 deamon來模擬2臺服務器進行主從復制:

$ mkdir m_master m_slave

$mongodb/bin/mongod--port28018 --dbpath ~/m_master--master&

$mongodb/bin/mongod--port28019 --dbpath ~/m_slave--slave--sourcelocalhost:28018&

這樣主從服務器都已經(jīng)啟動了,可以利用 netstat -an -t 查看28018、28019端口 是否開放

登錄主服務器:

$ mongodb/bin/mongo --port 28018

MongoDB shell version: 1.2.4-

url: test

connecting to: 127.0.0.1:28018/test

type "help" for help

> show dbs

admin

local

test

> use test

switched to db test

> show collections

這里主上的test數(shù)據(jù)什么表都沒有,為空,查看從服 務器同樣也是這樣

$ mongodb/bin/mongo --port 28019

MongoDB shell version: 1.2.4-

url: test

connecting to: 127.0.0.1:28019/test

type "help" for help

> show dbs

admin

local

test

> use test

switched to db test

> show collections

那么現(xiàn)在我們來驗證主從數(shù)據(jù)是否會像想象的那樣同步 呢?

我們在主上新建表user

> db

test

>db.createCollection("user");

> show collections

system.indexes

user

>

表 user已經(jīng)存在了,而且test庫中還多了一個system.indexes用來存放索引的表

到從服務器上查看test庫:

> db

test

> show collections

system.indexes

User

> db.user.find();

>

從 服務器的test庫中user表已經(jīng)存在,同時我還查了一下user表為空

現(xiàn)在我們再來測試一下,向主服務器test庫的user表中插入一條數(shù)據(jù)

> show collections

system.indexes

user

> db.user.insert({uid:1,name:"Falcon.C",age:25});

> db.user.find();

{ "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }

>

這 時我們查看從服務器的test庫user表時會多出一條記錄來:

> db.user.find();

{ "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }

>

MongoDB 還有 Replica Pairs 和 Master - Master

參考地址:http://www.mongodb.org/display/DOCS/Master+Slave

MongoDB一般情況下都可以支持主主復制,但是在大部分情況下官方不推薦使用

運行 的master - master的準備工作是:

新建存放數(shù)據(jù) 庫文件 的路徑

$mkdir mongodata/mm_28050 mongodata/mm_28051

運行mongodb數(shù)據(jù)庫 ,一個端口 為:28050,一個為:28051

$ mongodb/bin/mongod --port 28050 --dbpath ~/mongodata/mm_28050 --master --slave --source localhost:28051 > /dev/null &

$ mongodb/bin/mongod --port 28051 --dbpath ~mongodata/mm_28051 --master --slave --source localhost:28050 > /dev/null &

可以通過ps -ef|grep mongod 或 netstat -an -t來檢查是否運行功能

測試master - master模式 :

$ mongodb/bin/mongo --port 28050

MongoDB shell version: 1.2.4-

url: test

connecting to: 127.0.0.1:28050/test

type "help" for help

> show dbs

admin

local

> db

test

> db.user.insert({_id:1,username:"Falcon.C",age:25,sex:"M"});

> db.user.find();

{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

> db.user.find();//在28051端口插入數(shù)據(jù)后,再來查詢,看數(shù)據(jù)是否同步

{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }

>

$ mongodb/bin/mongo --port 28051

MongoDB shell version: 1.2.4-

url: test

connecting to: 127.0.0.1:28051/test

type "help" for help

> db

test

> show collections端口28050已經(jīng)新建了一個user表并插入了一條數(shù)據(jù),這里多出2表

system.indexes

user

> db.user.find();//查詢表user發(fā)現(xiàn)數(shù)據(jù)已經(jīng)同步

{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

> db.user.insert({_id:2,username:"NetOne",age:24,sex:"F"});在此插入數(shù)據(jù)看數(shù)據(jù)是否雙向同步

> db.user.find();

{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }

{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }

>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容