MongoDB——簡單增、刪、改、查實踐
1. 增刪改查
Insert新增一個文件到資料庫
#從oracle的emp表中生成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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB下載安裝與簡單增刪改查MongoDB
- CoreData - 簡單 增刪改查
- MongoDB增刪改查操作MongoDB
- MongoDB的增刪改查MongoDB
- mongodb 基本增刪改查MongoDB
- Java實現簡單的增刪改查操作Java
- Mybatis-plus實現簡單增刪改查MyBatis
- PHP操作MongoDB(增刪改查)PHPMongoDB
- layui+ssm簡單增刪改查UISSM
- 使用Mongoose類庫實現簡單的增刪改查Go
- Go微服務實踐之增刪改查Go微服務
- JavaAPI操作MongoDB--基本增刪改查JavaAPIMongoDB
- webpack4+express+mongodb+vue 實現增刪改查WebExpressMongoDBVue
- PLSQL學習-【2簡單的增刪改查】SQL
- 寫一個簡單的Linkedlist,實現增刪改查
- 單連結串列實現增刪改查
- 使用express+mongoose對mongodb實現增刪改查操作ExpressMongoDB
- python 連線mongodb實現增刪改查例項PythonMongoDB
- express+mongodb+vue實現增刪改查-全棧之路ExpressMongoDBVue全棧
- Spring Boot 中使用 MongoDB 增刪改查Spring BootMongoDB
- Node+Express+MySql實現簡單增刪改查和登入ExpressMySql
- 使用Django開發簡單介面:文章增刪改查Django
- express+mongodb+vue實現增刪改查-全棧之路2.0ExpressMongoDBVue全棧
- 增刪改查
- MongoDB入門(介紹、安裝、增刪改查)MongoDB
- 關於mongodb資料庫的增刪改查MongoDB資料庫
- 利用Express+MySQL進行簡單的增刪改查ExpressMySql
- 最簡單的sql語句(增刪改查統計)SQL
- 使用Go語言建立簡單的CRUD增刪改查Go
- Golang中map的三種宣告方式和簡單實現增刪改查Golang
- indexedDB 增刪改查Index
- SQL增刪改查SQL
- mysql增刪改查MySql
- Mongoose查增改刪Go
- FMDB增刪改查
- mysql增查刪改MySql
- Mongodb write寫(增、刪、改)模組原始碼實現MongoDB原始碼
- MongoDB工具類:java操作對檔案的增刪查改MongoDBJava