1排序sort()
a降序排列
db.c4.find().sort().sort({age:-1});
b升序排列:
db.c4.find().sort({age:-1});
c排序分頁
db.collectionname.find().sort({age:-1}).skip(20).limit(10);
eg:db.c4.find().sort({age:-1}).skip(20).limit(10);
2 $all,$in,$nin,$or,$nor,$exists
查詢集合中的文檔,$all主要用來查詢數組中的包含關系,查詢條件中隻要有一個不包含就不傳回。
$all(隻有都包含才會顯示)
> db.c5.insert({name:’zhangsan’,age:[1,2,3,4,5,9,10]});
writeresult({ “ninserted” : 1 })
> db.c5.insert({name:’lisi’,age:[2,4,7,9,10]});
> db.c5.insert({name:’wangwu’,age:[4,7,8,16,24]});
> db.c5.find({age:{$all:[4,7,9]}});
{ “_id” : objectid(“543e20c5e2c90313035e7d06”), “name” : “lisi”, “age” : [ 2, 4, 7, 9, 10 ] }
>
$in(隻要有包含1個就顯示)
> db.c5.find();
{ “_id” : objectid(“543e2098e2c90313035e7d05”), “name” : “zhangsan”, “age” : [ 1, 2, 3, 4, 5, 9, 10
] }
{ “_id” : objectid(“543e20ece2c90313035e7d07”), “name” : “wangwu”, “age” : [ 4, 7, 8, 16, 24 ] }
>db.c5.find({age:{$in:[1,2,3]}});
$nin (表示不包含的才顯示)
>db.c5.find({age:{$nin:[1,2,3]}});
$or (相當于或)
> db.c6.find({$or:[{name:”zhangsan”},{age:25}]})
{ “_id” : objectid(“543e2409e2c90313035e7d08”), “name” : “zhangsan”, “age” : 24 }
{ “_id” : objectid(“543e2432e2c90313035e7d09”), “name” : “lisi”, “age” : 25 }
{ “_id” : objectid(“543e2451e2c90313035e7d0a”), “name” : “wangwu”, “age” : 25 }
$nor
查詢集合中的文檔,$nor,表示根據條件過濾掉某些資料,例如:查詢name不是user2,age不是3的文檔,指令為:
db.collectionname.find({$nor:[{name:”user1”},{age:3}]});
> db.c6.find();
> db.c6.find({$nor:[{name:”zhangsan”},{age:25}]})
$exists
查詢集合中的文檔,$exists,用于查詢集合中存在某個鍵的文檔或不存在某個鍵的文檔,例如查詢customer集合中存在name鍵的所有文檔,可以使用db.collectionname.find({name:{$exists:1}});
$exists:1表示真,指存在
$exists:0表示假,指不存在
分别查詢包含name屬性的和包含sex屬性的資料
3遊标
遊标(相當于資料庫的一個臨時的存儲區,它是存放在記憶體中的,查找某個集合傳回記錄,我可以把這些記錄放在遊标當中,然後我可以疊代遊标)。
查詢集合中的文檔,類似關系型資料庫,mongodb中也有遊标的概念
4更新
> show dbs;
admin (empty)
local 0.078gb
test 0.078gb
> use toto
switched to db toto
> db
toto
> use test
switched to db test
> db.c1.insert({name:"zhangsan",age:19});
writeresult({ "ninserted" : 1 })
> db.c1.insert({name:"lis",age:25});
> db.c1.insert({name:"wangwu",age:23});
> db.c1.insert({name:"zhaoliu",age:27});
> db.c1.insert({name:"tianqi",age:33});
> db.c1.find();
{ "_id" : objectid("543e3223e2c90313035e7d0e"), "name" : "zhangsan", "age" : 19 }
{ "_id" : objectid("543e3244e2c90313035e7d0f"), "name" : "lis", "age" : 25 }
{ "_id" : objectid("543e3259e2c90313035e7d10"), "name" : "wangwu", "age" : 23 }
{ "_id" : objectid("543e3270e2c90313035e7d11"), "name" : "zhaoliu", "age" : 27 }
{ "_id" : objectid("543e3294e2c90313035e7d12"), "name" : "tianqi", "age" : 33 }
更新操作:
db.collectionname.update({條件},{修改的值}),函數中4個參數的介紹
1. 設定更新的條件
2. 設定更新的内容的對象
3. 如果沒有符合條件的記錄,是否新增一條記錄(取值為0/1)
4. 如果有多條記錄符合,是否全部更新(取值0/1)
db.collectionname.update({條件},{修改的值})
1. 更新的時候隻更新到一條,且其它的key:value覆寫掉了
2. 我隻更新到一條
{ "_id" : objectid("543e3244e2c90313035e7d0f"), "age" : 36 }
> db.c1.update({age:36},{$set:{name:"zhangsan"}},0,1);
writeresult({ "nmatched" : 1, "nupserted" : 0, "nmodified" : 1 })
{ "_id" : objectid("543e3244e2c90313035e7d0f"), "age" : 36, "name" : "zhangsan" }
> db.c1.update({age:24},{$set:{name:"tuzuoquan",age:24}},1,0);
writeresult({
"nmatched" : 0,
"nupserted" : 1,
"nmodified" : 0,
"_id" : objectid("543e3644dd76b7dcaba68faf")
})
{ "_id" : objectid("543e3644dd76b7dcaba68faf"), "age" : 24, "name" : "tuzuoquan" }
5 inc
更新集合中的文檔,使用$inc将集合中name為user1的age加1,其它鍵不變,$inc表示使某個鍵值加減指定的數值。
> for(var i=1;i<=20;i++){ db.c2.insert({name:"tuzuoquan",age:24}); }
> db.c2.find();
{ "_id" : objectid("543e386ee2c90313035e7d13"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e386ee2c90313035e7d14"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d15"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d16"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d17"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d18"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d19"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1a"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1b"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1c"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1d"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1e"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d1f"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d20"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d21"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d22"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d23"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d24"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d25"), "name" : "tuzuoquan", "age" : 24 }
{ "_id" : objectid("543e38a5e2c90313035e7d26"), "name" : "tuzuoquan", "age" : 24 }
db.c2.update({name:"tuzuoquan"},{$inc:{age:1}},0,1);
6 $unset(删除某個鍵)
更新集合中的文檔,$unset用來删除某個鍵,例如删除name為user1的文檔中address鍵,可以使用指令:
db.c1.update({name:”user”},{$unset:{age:1}},0,1);
>db.c2.update({name:"tuzuoquan"},{$unset:{age:1}},0,1);
writeresult({ "nmatched" : 20, "nupserted" : 0, "nmodified" : 20 })
{ "_id" : objectid("543e386ee2c90313035e7d13"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e386ee2c90313035e7d14"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d15"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d16"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d17"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d18"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d19"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1a"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1b"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1c"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1d"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1e"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d1f"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d20"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d21"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d22"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d23"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d24"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d25"), "name" : "tuzuoquan" }
{ "_id" : objectid("543e38a5e2c90313035e7d26"), "name" : "tuzuoquan" }