標籤:
操作記錄與總結:
show dbs
use SOME_DB(既是選定某個DB,又可以建立一個新的DB【注意要寫入資料,新的SOME_DB不可為空白】)
show collections
$inc
> db.simple.find(){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 12 }{ "_id" : ObjectId("54e9dc37a4c00ba20f4432ae"), "name" : "bcd", "age" : 13 }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }> db.simple.update({"name":"abc"},{$inc:{"sex":"none"}})WriteResult({"nMatched" : 0,"nUpserted" : 0,"nModified" : 0,"writeError" : {"code" : 14,"errmsg" : "Cannot increment with non-numeric argument: {sex: \"none\"}"}})> db.simple.update({"name":"abc"},{$inc:{"age":50}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.simple.find(){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 62 }{ "_id" : ObjectId("54e9dc37a4c00ba20f4432ae"), "name" : "bcd", "age" : 13 }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }
總結:$inc後只能跟數字,用於數值的遞增
$set
> db.simple.update({"name":"abc"},{$set:{"sex":"none"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.simple.find(){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 62, "sex" : "none" }{ "_id" : ObjectId("54e9dc37a4c00ba20f4432ae"), "name" : "bcd", "age" : 13 }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }>
總結:$set後可以為字串之類的東西
$in,$or,$nin
> db.simple.find({"wife":{$in:["jc"]}}){ "_id" : ObjectId("54ec289e560c1ea13477ce89"), "name" : "xul", "wife" : [ "jc", "juhua" ] }{ "_id" : ObjectId("54ec28b8560c1ea13477ce8a"), "name" : "jack", "wife" : [ "jc", "huangrui" ] }> db.simple.find({"wife":{$nin:["jc"]}}){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 62, "sex" : "none" }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }{ "_id" : ObjectId("54ec259d560c1ea13477ce85"), "name" : "def", "age" : 14 }{ "_id" : ObjectId("54ec25ac560c1ea13477ce86"), "name" : "abc", "age" : 16 }> db.simple.find({"wife":{$in:["huangrui"]}}){ "_id" : ObjectId("54ec28b8560c1ea13477ce8a"), "name" : "jack", "wife" : [ "jc", "huangrui" ] }> db.simple.find({"wife":{$in:["juhua"]}}){ "_id" : ObjectId("54ec289e560c1ea13477ce89"), "name" : "xul", "wife" : [ "jc", "juhua" ] }> db.simple.find({$or:[{"age":14},{"age":16}]}){ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54ec259d560c1ea13477ce85"), "name" : "def", "age" : 14 }{ "_id" : ObjectId("54ec25ac560c1ea13477ce86"), "name" : "abc", "age" : 16 }> > > db.simple.find({"wife":{$nin:["jc"]}}){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 62, "sex" : "none" }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }{ "_id" : ObjectId("54ec259d560c1ea13477ce85"), "name" : "def", "age" : 14 }{ "_id" : ObjectId("54ec25ac560c1ea13477ce86"), "name" : "abc", "age" : 16 }{ "_id" : ObjectId("54ec38ab560c1ea13477ce8b"), "name" : "jc", "hus" : [ "shit" ] }{ "_id" : ObjectId("54ec38be560c1ea13477ce8c"), "name" : "juhua", "wife" : [ "shit" ] }>
不要在意那些奇葩的資料!
總結:沒什麼好說的,就是沒有$not
count:
> db.simple.count({"age":14})2> db.simple.count({"age":16})2
distinct:去重
> db.simple.find(){ "_id" : ObjectId("54e9dc1fa4c00ba20f4432ad"), "name" : "abc", "age" : 62, "sex" : "none" }{ "_id" : ObjectId("54e9dc52a4c00ba20f4432af"), "name" : "cde", "age" : 14 }{ "_id" : ObjectId("54e9dc64a4c00ba20f4432b0"), "name" : "def", "age" : 16 }{ "_id" : ObjectId("54e9dc75a4c00ba20f4432b1"), "name" : "def", "age" : 656 }{ "_id" : ObjectId("54ec259d560c1ea13477ce85"), "name" : "def", "age" : 14 }{ "_id" : ObjectId("54ec25ac560c1ea13477ce86"), "name" : "abc", "age" : 16 }{ "_id" : ObjectId("54ec289e560c1ea13477ce89"), "name" : "xul", "wife" : [ "jc", "juhua" ] }{ "_id" : ObjectId("54ec28b8560c1ea13477ce8a"), "name" : "jack", "wife" : [ "jc", "huangrui" ] }> db.simple.disinct("age)2015-02-24T16:14:03.415+0800 SyntaxError: Unexpected token ILLEGAL> db.simple.disinct("age")2015-02-24T16:14:05.554+0800 TypeError: Property ‘disinct‘ of object name.simple is not a function> db.simple.distinct("age")[ 62, 14, 16, 656 ]> db.simple.distinct("name")[ "abc", "cde", "def", "xul", "jack" ]>
另一篇參考:
常用命令:http://www.cnblogs.com/cxd4321/archive/2011/06/24/2089051.html
#mongodb#速成筆記