mongodb基本操作

1、创建数据库

启动数据库后(配置文件是/Users/thatway/mongodata/etc/mongo.conf)

mongod -f /Users/thatway/mongodata/etc/mongo.conf

命令行进入mongodb模式

userdeiMac-2:~ ThatWay$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2018-02-26T10:54:30.021+0800 I CONTROL  [initandlisten]
2018-02-26T10:54:30.021+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-26T10:54:30.021+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-02-26T10:54:30.021+0800 I CONTROL  [initandlisten]
>

显示已有的数据库

show dbs

创建网站数据库(net)
use 数据库名

> use net
switched to db net

删除数据库,使用db.dropDatabase(),比如要删除demo数据库

> show dbs
admin  0.000GB
demo   0.000GB
local  0.000GB
net    0.000GB
> use demo
switched to db demo
> db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }
> show dbs
admin  0.000GB
local  0.000GB
net    0.000GB

mongodb中的操作都是先use数据库,然后再用比如db.xxx、show xxx等来对当前数据库进行相应操作。

2、创建集合

集合相当于是关系型数据库中的表,集合中是一个个文档,文档相当于是关系型数据库中的行,在mongodb中就是一个个json格式的数据。
向网站数据库(net)中添加资讯集合(news)
使用db.db.createCollection(‘集合名’)

> db.createCollection('news')
{ "ok" : 1 }

或者还有一种方式可以创建集合,就是直接向一个还没有的集合中插入数据,会自动创建这个集合,比如创建test集合

> db.test.insert({name:'thatway'})
WriteResult({ "nInserted" : 1 })

查看当前数据的集合

> show collections
news
test

删除集合

db.集合名.drop()

3、增删改查

  • 1、新增数据
> db.news.insert({title:'资讯',content:'集福今天晚上开奖',createDate:'2018-2-15'})
WriteResult({ "nInserted" : 1 })
  • 2、修改数据
> db.news.update({title:'资讯'},{$set:{title:'这是一条资讯'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

第一个参数是条件,条件也是一个对象,用于查找要修改的集合中的数据的范围,第二个参数是要修改成的值,$set表示要设值。

  • 3、查询数据
    查看集合中的所有数据
> db.news.find()
{ "_id" : ObjectId("5a938008a3dce7a4759846bb"), "title" : "这是一条资讯", "content" : "集福今天晚上开奖", "createDate" : "2018-2-15" }

格式化结果

> db.news.find().pretty()
{
    "_id" : ObjectId("5a938008a3dce7a4759846bb"),
    "title" : "这是一条资讯",
    "content" : "集福今天晚上开奖",
    "createDate" : "2018-2-15"
}

查看第一条

> db.news.findOne()
{
    "_id" : ObjectId("5a938008a3dce7a4759846bb"),
    "title" : "这是一条资讯",
    "content" : "集福今天晚上开奖",
    "createDate" : "2018-2-15"
}

带条件

> db.news.find({title:'这是一条资讯'})
{ "_id" : ObjectId("5a938008a3dce7a4759846bb"), "title" : "这是一条资讯", "content" : "集福今天晚上开奖", "createDate" : "2018-2-15" }

大于、大于等于、小于、小于等于

> db.user.insert({name:'thatway',age:20})
WriteResult({ "nInserted" : 1 })
> db.user.insert({name:'wp',age:50})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }
{ "_id" : ObjectId("5a939035a3dce7a4759846bd"), "name" : "wp", "age" : 50 }

> db.user.find({age:{$gt:20}})
{ "_id" : ObjectId("5a939035a3dce7a4759846bd"), "name" : "wp", "age" : 50 }

> db.user.find({age:{$gte:20}})
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }
{ "_id" : ObjectId("5a939035a3dce7a4759846bd"), "name" : "wp", "age" : 50 }

> db.user.find({age:{$lt:50}})
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }

> db.user.find({age:{$lte:50}})
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }
{ "_id" : ObjectId("5a939035a3dce7a4759846bd"), "name" : "wp", "age" : 50 }
>
  • 4、删除数据
> db.user.find()
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }
{ "_id" : ObjectId("5a939035a3dce7a4759846bd"), "name" : "wp", "age" : 50 }

> db.user.remove({age:{$gt:20}})
WriteResult({ "nRemoved" : 1 })

> db.user.find()
{ "_id" : ObjectId("5a93902aa3dce7a4759846bc"), "name" : "thatway", "age" : 20 }

4、用户鉴权

启动数据库时可以通过–auth选项,指定访问数据库需要用户鉴权。

  • 1、创建用户
    在非鉴权模式下启动mongodb:
mongod -f /Users/thatway/mongodata/etc/mongo.conf

准备为net数据库创建用户

> use net
switched to db net

查看已有用户

> show users

创建用户:

> db.createUser({user:'master',pwd:'123456',roles:['dbOwner']})
Successfully added user: { "user" : "master", "roles" : [ "dbOwner" ] }

认证用户:

> db.auth('master','123456')
1

用鉴权方式启动数据库

mongod -f /Users/thatway/mongodata/etc/mongo.conf --auth

进入mongo终端,查看net数据库的集合,出现权限不通过问题

 use net
switched to db net
> show collections
2018-02-26T12:57:35.589+0800 E QUERY    [thread1] Error: listCollections failed: {
    "ok" : 0,
    "errmsg" : "not authorized on net to execute command { listCollections: 1.0, filter: {} }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:807:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:819:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:830:16
shellHelper.show@src/mongo/shell/utils.js:762:9
shellHelper@src/mongo/shell/utils.js:659:15
@(shellhelp2):1:1

认证用户:

> db.auth('master','123456')
1

再查看集合,成功:

> show collections
news
test
user

创建用户时使用的dbOwner角色是mongoDb的预置角色,也可以自定义角色。
mongodb中有一个默认库admin,存有数据库管理员数据,为admin库添加的用户为数据库管理员账号,在鉴权模式下,只有拥有数据库管理员账号或者当前库管理员账号,才能为库添加用户。
mongodb角色介绍:
http://blog.csdn.net/dazuiba008/article/details/70210961

5、可视化工具

windows平台使用mongovue比较多,主页: http://www.mongovue.com/
mac平台使用MongoHub比较多,主页:http://mongohub.todayclose.com/
MongoHub安装成功后,不知道是版本的问题还是电脑环境的问题,打不开数据库中的集合,安装了一个RoboMongo,比较好用,主页:https://robomongo.org/,下载Robo 3T。

6、数据导入

  • 1、导入使用mongoimport
mongoimport -h 127.0.0.1:27017 -u mall -p 123456 -d mall -c goods --file /Users/thatway/mongodata/temp/mallgoods

参数说明:


    h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
    --port:代表远程连接的数据库的端口,默认连接的远程端口27017    -u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
    -p,--password:代表连接数据库的账号对应的密码;
    -d,--db:代表连接的数据库;
    -c,--collection:代表连接数据库中的集合;
    -f, --fields:代表导入集合中的字段;
    --type:代表导入的文件类型,包括csv和json,tsv文件,默认json格式;
    --file:导入的文件名称
    --headerline:导入csv文件时,指明第一行是列名,不需要导入;
  • 2、导出使用mongoexport
mongoexport -h 127.0.0.1:27017 -u mall -p 123456 -d mall -c users -o /Users/thatway/mongodata/temp/mallusers2 --type json

参数说明:


    -h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
    --port:代表远程连接的数据库的端口,默认连接的远程端口27017    -u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
    -p,--password:代表连接数据库的账号对应的密码;
    -d,--db:代表连接的数据库;
    -c,--collection:代表连接数据库中的集合;
    -f, --fields:代表集合中的字段,可以根据设置选择导出的字段;
    --type:代表导出输出的文件类型,包括csv和json文件;
    -o, --out:代表导出的文件名;
    -q, --query:代表查询条件;
     --skip:跳过指定数量的数据;
    --limit:读取指定数量的数据记录;
    --sort:对数据进行排序,可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列,如sort({KEY:1})。

注意:当查询时同时使用sort,skip,limit,无论位置先后,最先执行顺序 sort再skip再limit。

mongodb更多操作介绍:
http://blog.csdn.net/qq80583600/article/details/75047811

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值