mongodb 数据库基础实践
2017-11-09 15:07
381 查看
mongodb数据库基础操作,包括数据库的创建,查询,检索等等操作,实验使用实验楼的mongodb的基础教程中管理员工基础信息的测试。
基本操作方法是
mongo启动
创建数据库
创建文档
查看数据库和文档
添加集合
更新几何
创建索引
mongoexport导出json文件,需先推出mongo环境,执行exit,
基本格式为
mongoimport导入操作
附录:
实验楼的实验地址:https://www.shiyanlou.com/courses/12
基本操作方法是
mongo启动
mongo
创建数据库
use shiyanlou
创建文档
db.createCollection('employee')
查看数据库和文档
show dbs show collections
添加集合
employee1 = ({'sid':1001,'sname':'Tom','age':35,'gender':'male','phone':'13981234567','address':[{'city':'Chengdu','district':'jinniu'}]}) employee2 = ({'sid':1002,'sname':'Jack','age':26,'gender':'male','phone':'13981357913','address':[{'city':'Chengdu','district':'wudou'}]}) employee3 = ({'sid':1003,'sname':'Rose','age':31,'gender':'female','phone':'13980246802','address':[{'city':'Chongqing','district':'jiangbei'}]}) employee4 = ({'sid':1004,'sname':'Bob','age':29,'gender':'male','phone':'13987654321','address':[{'city':'Chongqing','district':'yuzhong'}]}) employee5 = ({'sid':1005,'sname':'Gavin','age':24,'gender':'male','phone':'13989753197','address':[{'city':'Chengdu','district':'jinniu'}]}) employee6 = ({'sid':1006,'sname':'Amy','age':27,'gender':'female','phone':'13988642086','address':[{'city':'Shanghai','district':'xuhui'}]}) employee7 = ({'sid':1007,'sname':'Anne','age':23,'gender':'female','phone':'18211237894','address':[{'city':'Chengdu','district':'qingyang'}]}) employee8 = ({'sid':1008,'sname':'John','age':33,'gender':'male','phone':'18219638521','address':[{'city':'Chongqing','district':'jiangbei'}]}) employee9 = ({'sid':1009,'sname':'Tony','age':36,'gender':'male','phone':'18211478523','address':[{'city':'Chengdu','district':'jinniu'}]}) employee10 = ({'sid':1010,'sname':'Betty','age':28,'gender':'female','phone':'18218520369','address':[{'city':'Beijing','district':'chaoyang'}]}) db.employee.insert([employee1,employee2,employee3,employee4,employee5,employee6,employee7,employee8,employee9,employee10])
更新几何
db.employee.update({'sname':'Tom'},{$set:{'phone':'18200753159','address':[{'city':'chengdu','district':'chenghua'}]}})
创建索引
db.employee.ensureIndex({'sid':1,'sname':1})
mongoexport导出json文件,需先推出mongo环境,执行exit,
mongoexport -d shiyanlou -c employee -q '{age: {$gte:30}}' --out tmp/employee.json
基本格式为
mongoexport命令行用于数据的导出,默认导出的文件格式为JSON格式。当然也可以指定特定的文件格式。 2、语法 [html] view plain copy C:\mongo\bin>mongoexport -help options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb if dbpath specified, each db is in a separate directory -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) -f [ --fields ] arg comma separated list of field names e.g. -f name,age --fieldFile arg file with fields names - 1 per line -q [ --query ] arg query filter, as a JSON string --csv export to csv instead of json -o [ --out ] arg output file; if not specified, stdout is used --jsonArray output to a json array rather than one object per Line 说明: -h:数据库宿主机的IP -u:数据库用户名 -p:数据库密码 -d:数据库名字 -c:集合的名字 -f:导出的列名 -q:导出数据的过滤条件 --csv:导出格式为csv -o:指明到要导出的文件名
mongoimport导入操作
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示: 2.语法 [root@localhost mongodb]# ./bin/mongoimport --help options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb if dbpath specified, each db is in a separate directory --journal enable journaling -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) -f [ --fields ] arg comma separated list of field names e.g. -f name,age --fieldFile arg file with fields names - 1 per line --ignoreBlanks if given, empty fields in csv and tsv will be ignored --type arg type of file to import. default: json (json,csv,tsv) --file arg file to import from; if not specified stdin is used --drop drop collection first --headerline CSV,TSV only - use first line as headers --upsert insert or update objects that already exist --upsertFields arg comma-separated fields for the query part of the upsert. You should make sure this is indexed --stopOnError stop importing at first error rather than continuing --jsonArray load a json array, not one item per line. Currently limited to 4MB. 参数说明: -h:指明数据库宿主机的IP -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -f:指明要导入那些列
附录:
实验楼的实验地址:https://www.shiyanlou.com/courses/12
相关文章推荐
- SQL Server 2008中文版关系数据库基础与实践教程pdf
- 初学MongoDB实践笔记——安装、创建数据库、保存及查询数据
- 数据库基础与应用实践教程pdf
- Mongodb基础篇(二)之安装数据库和开启相应的服务
- MongoDB 与关系型数据库(Oracle)关联统计实践方案(续)
- 干货:数据库分库分表基础和实践
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
- 非关系数据库—MongoDB基础总结
- 数据库-mongodb-基础
- awt实现登陆界面+数据库——学java,零基础不怕,不只要理论,更要实践+项目<a href=http://www.bjweixin.com>太原维信科技提供 </a>
- 第五十一课 NoSQL基础概念及MongoDB应用、数据库分配概念
- 手把手0基础项目实战 · 微服务架构下的数据库分库分表实践
- mongodb基础系列——数据库查询数据返回前台JSP(二)
- mongodb和pymongo创建数据库和基础操作
- MongoDB 与关系型数据库(Oracle)关联统计实践方案
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
- Mongodb基础实践(二)
- APP后台开发运维与架构实践 8 :MongoDB --- App后台新兴的数据库
- mongodb基础系列——数据库查询数据返回前台JSP(一)
- MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录