【mongodb】mongodb的基本使用

发表于2015-10-28 15:56  |  次阅读  |  0条评论  |   作者:siru90

【1】连接数据库
#使用mongod命令建立一个mongodb数据库链接,端口号设置为100001,数据库的路径为/mongodb/data,日志路径为/mongodb/log/mongodb.log

[root@localhost ~]# cd  /src/mongodb
[root@localhost mongodb]# ./bin/mongod -port 10001 --dbpath data/ --logpath log/mongodb.log  
all output going to: log/mongodb.log 

#重新开启一个终端,然后切换到mongodb目录下:
[root@localhost /]# cd /src/mongodb  
 
#然后使用bin/mongo命令来连接该数据库
[root@localhost mongodb]# ./bin/mongo localhost:10001  
MongoDB shell version: 2.0.2-rc2  
connecting to: localhost:10001/test 

【2】查询数据库
> show dbs
admin   (empty)
local   (empty)
 
【3】切换数据库
> use mongo  #切换数据库
switched to db mongo

> show tables  #显示表

【4】基本操作
#插入数据
> db.mongo.insert({x:11})  # 这样写入会自动生成一个mongo集合
> show tables
mongo
system.indexes
> db.mongo.find()
{ "_id" : ObjectId("56268c22d62feb78a7a09a23"), "x" : 11 }

#在mongo数据库中 建立userid集合
> db.mongo.userid.insert({userid:1})
> show tables
mongo
mongo.userid
system.indexes
> db.mongo.userid.find()  #查找userid集合中的数据
{ "_id" : ObjectId("56268c6cd62feb78a7a09a24"), "userid" : 1 }


【总结】
db.test.insert({x:1})   #插入数据
db.test.find() #查找test表中所有的数据
db.test.find({x:1}) #带条件查询
db.test.find().count()  #查询记录总条数
db.test.update({x:1},{x:2}) #f更新数据{x:1}为{x:2}
db.test.update({x:1},{x:2},boolean,boolean)  第三个参数:如果数据不存在则插入, 第四个参数:true表示批量更新
db.test.remove({x:1}) #删除,必须带条件
db.test.drop() #删除表
show tables #显示表
 
本站关键字:sunny90 web开发 数据库 移动开发 服务器 Nginx Mysql PHP
Copyright © sunny90版权所有 power by sunny90.com  
湘ICP备14012284号-1,粤公网安备 44030602000307号