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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27000195/viewspace-1400261/