本文转自 http://my.oschina.net/Kenyon/blog/99742
一、MongoDB的shell登陆
[mongo @localhost ~]$ mongo MongoDB shell version: 2.2.2 connecting to: test >
二、查看现有的数据库
> show dbs; local (empty) test 0.203125GB三、数据库切换
> use local; switched to db local > use test; switched to db test >四、显示collctions (感觉和DBMS的table比较像,但差别很大)
> show collections; kenyon system.indexes test things >五、数据插入
方式一(save): > db.kenyon.save({id:8899,name:'kenyon_test_mongo'}); > db.kenyon.save({id:99,name:'kenyon_test_mongo',remark:'first time here'}); 方式二(预定义): > tmp={id:0909,name:'kenyon_test',create_time:'2013-01-01'}; > db.kenyon.insert(tmp); 方式三(循环插): > for (var i = 1; i <= 25; i++) db.tab_kenyon.insert( { x : 4 , j : i } ) > db.tab_kenyon.find(); { "_id" : ObjectId("50e4b8007a86419166fd68f0"), "x" : 4, "j" : 1 } { "_id" : ObjectId("50e4b8007a86419166fd68f1"), "x" : 4, "j" : 2 } { "_id" : ObjectId("50e4b8007a86419166fd68f2"), "x" : 4, "j" : 3 } { "_id" : ObjectId("50e4b8007a86419166fd68f3"), "x" : 4, "j" : 4 } { "_id" : ObjectId("50e4b8007a86419166fd68f4"), "x" : 4, "j" : 5 } { "_id" : ObjectId("50e4b8007a86419166fd68f5"), "x" : 4, "j" : 6 } { "_id" : ObjectId("50e4b8007a86419166fd68f6"), "x" : 4, "j" : 7 } { "_id" : ObjectId("50e4b8007a86419166fd68f7"), "x" : 4, "j" : 8 } { "_id" : ObjectId("50e4b8007a86419166fd68f8"), "x" : 4, "j" : 9 } { "_id" : ObjectId("50e4b8007a86419166fd68f9"), "x" : 4, "j" : 10 } { "_id" : ObjectId("50e4b8007a86419166fd68fa"), "x" : 4, "j" : 11 } { "_id" : ObjectId("50e4b8007a86419166fd68fb"), "x" : 4, "j" : 12 } { "_id" : ObjectId("50e4b8007a86419166fd68fc"), "x" : 4, "j" : 13 } { "_id" : ObjectId("50e4b8007a86419166fd68fd"), "x" : 4, "j" : 14 } { "_id" : ObjectId("50e4b8007a86419166fd68fe"), "x" : 4, "j" : 15 } { "_id" : ObjectId("50e4b8007a86419166fd68ff"), "x" : 4, "j" : 16 } { "_id" : ObjectId("50e4b8007a86419166fd6900"), "x" : 4, "j" : 17 } { "_id" : ObjectId("50e4b8007a86419166fd6901"), "x" : 4, "j" : 18 } { "_id" : ObjectId("50e4b8007a86419166fd6902"), "x" : 4, "j" : 19 } { "_id" : ObjectId("50e4b8007a86419166fd6903"), "x" : 4, "j" : 20 } Type "it" for more > it { "_id" : ObjectId("50e4b8737a86419166fd6904"), "x" : 4, "j" : 21 } { "_id" : ObjectId("50e4b8737a86419166fd6905"), "x" : 4, "j" : 22 } { "_id" : ObjectId("50e4b8737a86419166fd6906"), "x" : 4, "j" : 23 } { "_id" : ObjectId("50e4b8737a86419166fd6907"), "x" : 4, "j" : 24 } { "_id" : ObjectId("50e4b8737a86419166fd6908"), "x" : 4, "j" : 25 }如果不存在collection会自动创建,不会报错,显示数据时默认是显示20条数据,输入it来查看下一页的20条数据
六、数据检索
1. 普通方式
> db.kenyon.find(); { "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" } { "_id" : ObjectId("50e49d2ecd320366f7122605"), "name" : "mongo", "id" : "888" } { "_id" : ObjectId("50e4b3fe7a86419166fd68ed"), "id" : 8899, "name" : "kenyon_test_mongo" } { "_id" : ObjectId("50e4b4167a86419166fd68ee"), "id" : 99, "name" : "kenyon_test_mongo", "remark" : "first time here" } { "_id" : ObjectId("50e4b6817a86419166fd68ef"), "id" : 909, "name" : "kenyon_test", "create_time" : "2013-01-01" }2.查询一条数据
> db.kenyon.findOne(); { "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" }3.条件检索
> db.kenyon.find(); { "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" } { "_id" : ObjectId("50e49d2ecd320366f7122605"), "name" : "mongo", "id" : "888" } { "_id" : ObjectId("50e4b3fe7a86419166fd68ed"), "id" : 8899, "name" : "kenyon_test_mongo" } { "_id" : ObjectId("50e4b4167a86419166fd68ee"), "id" : 99, "name" : "kenyon_test_mongo", "remark" : "first time here" } { "_id" : ObjectId("50e4b6817a86419166fd68ef"), "id" : 909, "name" : "kenyon_test", "create_time" : "2013-01-01" } > db.kenyon.find({id:909}); { "_id" : ObjectId("50e4b6817a86419166fd68ef"), "id" : 909, "name" : "kenyon_test", "create_time" : "2013-01-01" } > db.kenyon.find({name:"mongo"}); { "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" } { "_id" : ObjectId("50e49d2ecd320366f7122605"), "name" : "mongo", "id" : "888" }4.限制查询(limit)
> db.kenyon.find().limit(2) { "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" } { "_id" : ObjectId("50e49d2ecd320366f7122605"), "name" : "mongo", "id" : "888" }七、mongo的shell帮助
> help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell >