mongodb安装和实用命令

上传人:第*** 文档编号:32690769 上传时间:2018-02-12 格式:DOC 页数:7 大小:74KB
返回 下载 相关 举报
mongodb安装和实用命令_第1页
第1页 / 共7页
mongodb安装和实用命令_第2页
第2页 / 共7页
mongodb安装和实用命令_第3页
第3页 / 共7页
mongodb安装和实用命令_第4页
第4页 / 共7页
mongodb安装和实用命令_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《mongodb安装和实用命令》由会员分享,可在线阅读,更多相关《mongodb安装和实用命令(7页珍藏版)》请在金锄头文库上搜索。

1、命令可直接从文档复制。#后为命令从官网下载 mongodb-win32-i386-2.6.6.zip,把解压的文件放到 E:mongodb,备用。安装:1、配置环境变量 path,加上“;E:mongodbbin;”,在 E:mongodb 下建立文件夹 date-db,形成目录“E:mongodbdatadb”。2、(windows)doc 下打开两个 cmd 窗口(A 服务窗口、B 客户端窗口)。A 中输入#mongod -dbpath E:mongodbdatadb稍等片刻后-B 中输入#mongo当 A 中出现如下图类似字样,则说明连接成功。(亦是安装成功)默认连接端口为 27017M

2、ongodb 学习第二次启动 mongodb 服务。用 cmd 打开窗口(A 服务窗口、B 客户端窗口)。A 中输入命令:# mongod -dbpath=E:mongodbdatadb将 MongoDB 作为 Windows 服务随机启动:#mongod -dbpath=E:mongodbdatadb -logpath=E:mongodblogsmongodb.log -install若出现“32-bit servers dont have journaling enabled by default. Please use -journal if you want durability.”无

3、碍进入计算机-管理-服务,可看到有一个名为 MongoDB 的服务。右键“启动”即可。卸载服务:#mongod -dbpath=E:mongodbdatadb -remove -serviceName MongoDB进入计算机-管理-服务,之前名为 MongoDB 的服务不见了。B 中输入:#mongo创建用户 use admin 跳转进入 admin 用户下。 db.addUser(chenxi,chenxi123)出现“WARNING: The addUser shell helper is DEPRECATED. Please use createUser insteadSuccessf

4、ully added user: user : chenxi, roles : root ”无碍 db.auth(chenxi,chenxi123)1 用户校验成功,注:只是普通用户,0 为未匹配 db.removeUser(chenxi) 或 db.dropUser(chenxi)删除用户 db.addUser(admin,admin123)WARNING: The addUser shell helper is DEPRECATED. Please use createUser insteadSuccessfully added user: user : admin, roles : ro

5、ot use chenxi 转到 chenxi 用户下 db.addUser(padmin,padmin123) 创建普通存储用户Database 的相关操作 show dbs 显示数据库列表 show collections 显示当前数据库的集合( 类似关系数据库中的表) show users 显示用户(仅限当前级别下的 ) use local 转到 local 数据库下,格式为 use db.help() 显示数据库操作命令 db.foo.help()显示集合命令创建一个不存在的数据库 chenxitestdb use chenxitestdb创建一个名为“tmpusers”聚集集合 db

6、.createCollection(tmpusers) ok : 1 db.dropDatabase() dropped : chenxitestdb, ok : 1 当前使用的数据库被删除 db.cloneDatabase(127.0.0.1)将指定 IP 地址的数据库克隆到本地当前数据库 db.copyDatabase(chenxitest,chenxitestcp,127.0.0.1)将 IP 地址为 127.0.0.1 上的 chenxitest 数据库复制到本地 chenxitestcp 数据库中 db.repairDatabase()修复数据库,mysql 中不存在 db.stat

7、s()可查看当前数据库的状态,在 db.help()中可以找到 db.version()可以查看当前的 mongodb 版本 db.getMongo()connection to 127.0.0.1当前 db 连接机器的 IP 地址Collection 聚集集合 db.createCollection(mytestcoll, size : 20, capped : 5, max : 100 )创建一个名为 mytestcoll 的聚集集合,大小为 20 字节,最大为 100 字节,capped 是否为真表示为固定集合。 db.getCollection(account)得到指定名称的聚集集合(

8、table) db.getCollectionNames()得到当前 db 的所有聚集集合 db.printCollectionStats()显示当前 db 所有聚集索引状态其它 db.getPrevError() err : null, n : 1, nPrev : 4, ok : 1 db.resetError() ok : 1 查看聚集集合基本信息(前提:在 mytest 数据库下创建名为 mytestcoll 的聚集集合 db.mytestcoll.help()查看该集合下的帮助。 db.mytestcoll.count()查看当前集合下的数据条数0 db.mytestcoll.dat

9、aSize()查看当前数据空间大小0 db.mytestcoll.getDB()得到当前聚集集合所在的数据库名称Mytest db.mytestcoll.totalSize()得到聚集集合的总大小12272 db.mytestcoll.stats()得到将聚集集合的状态。 db.mytestcoll.storageSize()得到该聚集集合的存储空间(包括空闲的空间)4096 db.mytestcoll.getShardVersion() 得到用户 shard 版本信息。 db.mytestcoll.renameCollection(myrename)重命名聚集集合 ok : 1 show c

10、ollectionsMyrename(名字改了! )System.indexes db.mytestcoll.drop()删除名为 mytestcoll 聚集集合true聚集集合查询 show collectionsmytestcollsystem.indexes db.mytestcoll.find() 为空,因为 mytestcoll 中不存在记录。等价于 select * from mytestcoll db.system.indexes.find() v : 1, key : _id : 1 , name : _id_, ns : mytest.mytestcoll 默认每页显示 20

11、 条记录,当显示不下的情况下,可以用 it 迭代命令查询下一页数据。注意:键入 it 命令不能带“;”但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize= 50;这样每页就显示 50 条记录了。 db.mytestcoll.insert(key1:testkey1,key2:testkey2,key3:testkey3)在聚集集合中插入一个文档WriteResult( nInserted : 1 ) db.mytestcoll.insert(key1:testkey1,key3:testkey3_1,key2:testkey2_1)WriteResult( nIn

12、serted : 1 ) db.mytestcoll.find() _id : ObjectId(54b4b28860a9f83fc16e738d), key1 : testkey1, key2 : testkey2, key3 : testkey3 _id : ObjectId(54b4b32c60a9f83fc16e738e), key1 : testkey1, key3 : testkey3_1, key2 : testkey2_1 db.mytestcoll.save(key1:mytestkey1,key2:testkey2)也能保存一个文档到聚集集合WriteResult( nIn

13、serted : 1 ) DBQuery.shellBatchSize= 1 设置了每页显示几条记录,现在设置为 1 条1 db.mytestcoll.find() _id : ObjectId(54b4b28860a9f83fc16e738d), key1 : testkey1, key2 : testkey2, key3 : testkey3 Type it for more it 显示下一页的记录 _id : ObjectId(54b4b32c60a9f83fc16e738e), key1 : testkey1, key3 : testkey3_1, key2 : testkey2_1

14、db.mytestcoll.find(key2:testkey2_1) 以键值形式获取查询到的结果 相当于 select * from mytestcollwhere key2= “testkey2_1”; _id : ObjectId(54b4b32c60a9f83fc16e738e), key1 : testkey1, key3 : testkey3_1, key2 : testkey2_1 db.mytestcoll.find(key2: $lt: 22)查找 key2 db.mytestcoll.find(key2: $lte: 22)查找 key2 db.mytestcoll.fin

15、d(key2: $gt: 22)查找 key222 的值,相当于 select * from mytestcoll where key222; db.mytestcoll.find(key2: $gte: 22)查找 key2=22 的值,相当于 select * from mytestcoll where key2=22; db.mytestcoll.find(key2: $gt:11,$lte:22)查找 22=key211 的值 db.mytestcoll.find(key2:/test/)等价于 select * from mytestcoll where key2 like %test% _id : ObjectId(54b4b28860a9f83fc16e738d), key1 : test

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号