MongoDB——簡單增、刪、改、查實踐

jx_yu發表於2015-01-13

1.    增刪改查

Insert新增一個文件到資料庫

#oracleemp表中生成mongodb樣例資料

SQL> select 'doc={"empno":'||empno||','||'"ENAME":"'||ENAME||'",'||'"JOB":"'||JOB||'","MGR":'||MGR||',"SAL":'||SAL||'}'||chr(10)||'db.emp.insert(doc)' from emp;

'DOC={"EMPNO":'||EMPNO||','||'"ENAME":"'||ENAME||'",'||'"JOB":"'||JOB||'","MGR":'||MGR||',"SAL":'||SAL||'}'||CHR(10)||'DB.EMP.INSERT(DOC)'

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

doc={"empno":7369,"ENAME":"SMITH","JOB":"CLERK","MGR":7902,"SAL":800}

db.emp.insert(doc)

 

doc={"empno":7499,"ENAME":"ALLEN","JOB":"SALESMAN","MGR":7698,"SAL":1600}

db.emp.insert(doc)

 

doc={"empno":7521,"ENAME":"WARD","JOB":"SALESMAN","MGR":7698,"SAL":1250}

db.emp.insert(doc)

 

doc={"empno":7566,"ENAME":"JONES","JOB":"MANAGER","MGR":7839,"SAL":2975}

db.emp.insert(doc)

 

doc={"empno":7654,"ENAME":"MARTIN","JOB":"SALESMAN","MGR":7698,"SAL":1250}

db.emp.insert(doc)

 

doc={"empno":7698,"ENAME":"BLAKE","JOB":"MANAGER","MGR":7839,"SAL":2850}

db.emp.insert(doc)

 

doc={"empno":7782,"ENAME":"CLARK","JOB":"MANAGER","MGR":7839,"SAL":2450}

db.emp.insert(doc)

 

doc={"empno":7788,"ENAME":"SCOTT","JOB":"ANALYST","MGR":7566,"SAL":3000}

db.emp.insert(doc)

 

doc={"empno":7839,"ENAME":"KING","JOB":"PRESIDENT","MGR":,"SAL":5000}

db.emp.insert(doc)

 

doc={"empno":7844,"ENAME":"TURNER","JOB":"SALESMAN","MGR":7698,"SAL":1500}

db.emp.insert(doc)

 

doc={"empno":7876,"ENAME":"ADAMS","JOB":"CLERK","MGR":7788,"SAL":1100}

db.emp.insert(doc)

 

doc={"empno":7900,"ENAME":"JAMES","JOB":"CLERK","MGR":7698,"SAL":950}

db.emp.insert(doc)

 

doc={"empno":7902,"ENAME":"FORD","JOB":"ANALYST","MGR":7566,"SAL":3000}

db.emp.insert(doc)

 

doc={"empno":7934,"ENAME":"MILLER","JOB":"CLERK","MGR":7782,"SAL":1300}

db.emp.insert(doc)

find 檢視集合內容

#find 返回集合的所有文件,預設shell顯示最多20個匹配文件

> db.emp.find()

{ "_id" : ObjectId("54b395aaf254a092125491be"), "empno" : 7369, "ENAME" : "SMITH", "JOB" : "CLERK", "MGR" : 7902, "SAL" : 800 }

#除了我們得key-value對都完整的保留,額外新增健”_id”

{ "_id" : ObjectId("54b3961bf254a092125491bf"), "empno" : 7499, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }

{ "_id" : ObjectId("54b3961bf254a092125491c0"), "empno" : 7521, "ENAME" : "WARD", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }

{ "_id" : ObjectId("54b3961bf254a092125491c1"), "empno" : 7566, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }

{ "_id" : ObjectId("54b3961bf254a092125491c2"), "empno" : 7654, "ENAME" : "MARTIN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }

{ "_id" : ObjectId("54b3961bf254a092125491c3"), "empno" : 7698, "ENAME" : "BLAKE", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2850 }

{ "_id" : ObjectId("54b3961bf254a092125491c4"), "empno" : 7782, "ENAME" : "CLARK", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2450 }

{ "_id" : ObjectId("54b3961bf254a092125491c5"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491c6"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491c7"), "empno" : 7844, "ENAME" : "TURNER", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1500 }

{ "_id" : ObjectId("54b3961bf254a092125491c8"), "empno" : 7876, "ENAME" : "ADAMS", "JOB" : "CLERK", "MGR" : 7788, "SAL" : 1100 }

{ "_id" : ObjectId("54b3961bf254a092125491c9"), "empno" : 7900, "ENAME" : "JAMES", "JOB" : "CLERK", "MGR" : 7698, "SAL" : 950 }

{ "_id" : ObjectId("54b3961bf254a092125491ca"), "empno" : 7902, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491cb"), "empno" : 7934, "ENAME" : "MILLER", "JOB" : "CLERK", "MGR" : 7782, "SAL" : 1300 }

#findone檢視集合的一個文件

> db.emp.findOne()

{

        "_id" : ObjectId("54b395aaf254a092125491be"),

        "empno" : 7369,

        "ENAME" : "SMITH",

        "JOB" : "CLERK",

        "MGR" : 7902,

        "SAL" : 800

}

#where條件查詢

#empno=7844

> db.emp.find({"empno":7844})

{ "_id" : ObjectId("54b3961bf254a092125491c7"), "empno" : 7844, "ENAME" : "TURNER", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1500 }

#ename=ALLEN

> db.emp.find({"ENAME":"ALLEN"})

{ "_id" : ObjectId("54b3961bf254a092125491bf"), "empno" : 7499, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe0e"), "empno" : 8509, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }

# “條件1” and “條件2”

> db.emp.find({"ENAME":"ALLEN","empno" : 8509})

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe0e"), "empno" : 8509, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }

#查詢指定欄位的值

#select empno,sal from emp;

> db.emp.findOne({},{"empno":1,"SAL":1})

{

        "_id" : ObjectId("54b395aaf254a092125491be"),  #”_id”總會被返回,即使沒有指定

        "empno" : 7369,

        "SAL" : 800

}

#也可以指定不顯示的欄位

> db.emp.findOne({},{"_id":0})

{

        "empno" : 7369,

        "ENAME" : "SMITH",

        "JOB" : "CLERK",

        "MGR" : 7902,

        "SAL" : 800

}

#查詢條件

條件查詢有常用的:小於("$lt")、小於等於("$lte")、大於("$gt")、大於等於("$gte")、不等於("$ne") 

#select empno,sal from emp where sal>=1500 and sal<=2000;

> db.emp.find({"SAL":{"$gte":1500,"$lte":2000}},{"empno":1,"SAL":1,"_id":0})

{ "empno" : 7499, "SAL" : 1600 }

{ "empno" : 7844, "SAL" : 1500 }

{ "empno" : 8509, "SAL" : 1600 }

{ "empno" : 8854, "SAL" : 1500 }

#$ne可以用於所有資料型別

> db.emp.find({"empno":{"$ne":7369}})

> db.emp.find({"JOB":{"$ne":"CLERK"}})

#or查詢

"$in""$or" 。對單一鍵有多個值與其匹配的話就用"$in",後面跟一個條件陣列。

“$nin”是不在[]

#select * from emp where empno in (7499,7521)

> db.emp.find({"empno":{"$in":[7499,7521]}})

{ "_id" : ObjectId("54b3961bf254a092125491bf"), "empno" : 7499, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }

{ "_id" : ObjectId("54b3961bf254a092125491c0"), "empno" : 7521, "ENAME" : "WARD", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }

#sal=3000 or empno=8664

> db.emp.find({"$or":[{"SAL":3000},{"empno" : 8664}]})

{ "_id" : ObjectId("54b3961bf254a092125491c5"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491c6"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491ca"), "empno" : 7902, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe11"), "empno" : 8664, "ENAME" : "MARTIN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe14"), "empno" : 8798, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afcacff2ff00c53cfe15"), "empno" : 8798, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afcacff2ff00c53cfe19"), "empno" : 8912, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

#"$not"元條件句,用在其他任何條件上

#sal>=2500即查詢$not  sal小於等於2500的使用者 

> db.emp.find({"SAL":{"$not":{"$lt":2500}}})

{ "_id" : ObjectId("54b3961bf254a092125491c1"), "empno" : 7566, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }

{ "_id" : ObjectId("54b3961bf254a092125491c3"), "empno" : 7698, "ENAME" : "BLAKE", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2850 }

{ "_id" : ObjectId("54b3961bf254a092125491c5"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491c6"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b3961bf254a092125491ca"), "empno" : 7902, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe10"), "empno" : 8576, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe12"), "empno" : 8708, "ENAME" : "BLAKE", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2850 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe14"), "empno" : 8798, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afcacff2ff00c53cfe15"), "empno" : 8798, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

{ "_id" : ObjectId("54b4afcacff2ff00c53cfe19"), "empno" : 8912, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }

#查詢sal不是10的整數的行

> db.emp.find({"SAL":{"$not":{"$mod":[10,0]}}})

{ "_id" : ObjectId("54b3961bf254a092125491c1"), "empno" : 7566, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }

{ "_id" : ObjectId("54b4afc9cff2ff00c53cfe10"), "empno" : 8576, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }

等。。。。。。。

Delete 刪除文件

#delete emp where empno>8000

> db.emp.remove({"empno":{"$gt":8000}})

WriteResult({ "nRemoved" : 14 })

Update 更新文件

http://blog.csdn.net/qqiabc521/article/details/6325203

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1400261/,如需轉載,請註明出處,否則將追究法律責任。

相關文章