mongodb数据库基础操作,包括数据库的创建,查询,检索等等操作,实验使用实验楼的mongodb的基础教程中管理员工基础信息的测试。
基本操作方法是
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 ( /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 ( /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