mongodb安装过程与常用命令

参考博客:http://my.oschina.net/Kenyon/blog/99742


mongodb安装过程

1、解压缩

[root@localhost opt]# tar -zxvf mongodb-linux-i686-2.4.9.tgz

mongodb-linux-i686-2.4.9/README

mongodb-linux-i686-2.4.9/THIRD-PARTY-NOTICES

mongodb-linux-i686-2.4.9/GNU-AGPL-3.0

2、重命名文件夹

[root@localhost opt]# mv mongodb-linux-i686-2.4.9 mongodb

3、创建数据库日志文件夹和数据文件夹

[root@localhost mongodb]# mkdir log

[root@localhost mongodb]# mkdir data

4、启动服务

[root@localhost mongodb]# cd bin

[root@localhost bin]#

[root@localhost bin]# ./mongod --dbpath=/opt/mongodb/data --logpath=/opt/mongodb/log/mongodb.log --logappend &

[1] 16334

[root@localhost bin]# all output going to: /opt/mongodb/log/mongodb.log

5、注意安装的版本(32位还是64位)否则启动服务可能报下面的错误

[root@localhost bin]# ./mongod: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

mongodb常用命令

1、连接mongodb

[root@localhost opt]# cd mongodb/bin

[root@localhost bin]# ./mongo

MongoDB shell version: 2.4.9

connecting to: test

> use local

switched to db local

> show collections

ewalletlog

ewalletlogs

startup_log

system.indexes

>

> db.ewalletlogs.find();

2、查看现有的数据库> show dbs;
local   (empty)
test    0.203125GB

3、数据库切换> use local;
switched to db local
> use test;
switched to db test
>

4、显示collctions> show collections;
kenyon
system.indexes
test
things
>

5、数据插入方式一(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 }


如果不存在collection会自动创建,不会报错,显示数据时默认是显示20条数据,输入it来查看下一页的20条数据

6、数据检索

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) 通过正则表达式进行模糊查询

>db.ewalletlogs.find({message:/^2014-02-13 12:*/});

5)限制查询(limit)

> db.kenyon.find().limit(2)
{ "_id" : ObjectId("50e49c55f8de48037fe44062"), "name" : "mongo" }
{ "_id" : ObjectId("50e49d2ecd320366f7122605"), "name" : "mongo", "id" : "888" }


7、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


>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值