操作记录与总结:
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