db.hits.insert({"url":"www.datagru.cn","pv":102});
db.hits.insert({"url":"f.datagru.cn","pv":108});db.hits.insert({"url":"www.itpub","pv":155});> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 102 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }> db.hits.update({"url":"www.datagru.cn"},{$inc:{"pv":1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }$set直接设置> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "ip" : 20 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }$unset删除键值> db.hits.update({"url":"f.datagru.cn"},{$unset:{"ip":20}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }数组修改器db.hits.update({"url":"f.datagru.cn"},{"$set":{"users":[{"name":"hang","age":12},{"name":"huang","age":21}]}}) db.hits.update({"url":"f.datagru.cn"},{"$set":{"users":[{"name":"hang","age":12},{"name":"huang","age":21}]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 } ] }为数组增加元素db.hits.update({"url":"f.datagru.cn"},{"$push":{"users":{"name":"hang","age":12}}})> db.hits.update({"url":"f.datagru.cn"},{"$push":{"users":{"name":"hang","age":12}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 }, { "name" : "hang", "age" : 12 } ] }定位修改db.hits.update({"url":"f.datagru.cn"},{"$set":{"users.2.name":"liu"}})> db.hits.update({"url":"f.datagru.cn"},{"$set":{"users.2.name":"liu"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.hits.find();{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 }, { "name" : "liu", "age" : 12 } ] }upsert> db.runCommand({getLastError:1}){ "connectionId" : 1, "n" : 0, "syncMillis" : 0, "writtenTo" : null, "err" : null, "ok" : 1}mongodb查询finddb.abc.insert([{"a":23,"b":21,"c":34},{"a":23,"b":32},{"a":23,"b":24,"c":54},{"a":23,"c":37},{"a":31,"b":21,"c":37},{"a":31,"b":25},{"a":31,"c":34}])> db.abc.find(){ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "b" : 21, "c" : 34 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23, "b" : 32 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "b" : 24, "c" : 54 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435e"), "a" : 31, "b" : 21, "c" : 37 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435f"), "a" : 31, "b" : 25 }{ "_id" : ObjectId("56a21d2c6efa22a91f284360"), "a" : 31, "c" : 34 }子键筛选> db.abc.find({"a":23}){ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "b" : 21, "c" : 34 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23, "b" : 32 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "b" : 24, "c" : 54 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }> db.abc.find({"a":23},{"b":1}){ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "b" : 21 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "b" : 32 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "b" : 24 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435d") }> db.abc.find({"a":23},{"b":1},{"c":1})1感兴趣{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "b" : 21 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "b" : 32 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "b" : 24 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435d") }> db.abc.find({"a":23},{"b":0}){ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "c" : 34 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "c" : 54 }{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }> db.abc.find({"a":23},{"_id":0}){ "a" : 23, "b" : 21, "c" : 34 }{ "a" : 23, "b" : 32 }{ "a" : 23, "b" : 24, "c" : 54 }{ "a" : 23, "c" : 37 }> db.abc.find({"a":23},{"_id":0,"b":1})不能零和一混合除开_id{ "b" : 21 }{ "b" : 32 }{ "b" : 24 }{ }操作符$lt $lte db.abc.find({b:{"$gte":30,"$lt":35}})$gt $gte$all$exists$mod db.abc.find({b:{"$mod":[5,1]}})$ne$in db.abc.find({b:{"$in":[25,32]}})$nin$nor$or db.abc.find({"$or":[{"b":32},{"c":54}]})$size 元素个数$slice列出一部分元素$type