环境
MongoDB4.0
win7_x64
CentOS6.5_x64
一、MongoDB简介
(1)MongoDB使用C++开发。
(2)MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。
(3)MongoDB 将数据存储为一个文档。MongoDB是一个基于分布式文件存储的数据库。
(4)MongoDB使用BSON作为数据存储和传输的格式。BSON是一种类似JSON的二进制序列化文档,支持嵌套对象和数组。
(5)MongoDB很像MySQL,collection对应MySQL的table,document对应MySQL的row。官方给自己的定义是Key-value存储(高性能和高扩展)和传统RDBMS(丰富的查询和功能)之间的一座桥梁。
(6)MongoDB 不支持事务;
(7)MongoDB索引在内存中,而且创建受一定限制;
二、安装
1、win7安装
(1)官网下载:mongodb-win32-x86_64-2008plus-ssl-4.0.11.zip
(2)解压:D:mongoDB4.0
(3)通过服务安装,通过控制台安装:
进入目录:D:mongoDB4.0in,启动cmd:
mongod --bind_ip 127.0.0.1 --logpath "D:mongoDB4.0logmongodb.log" --logappend --dbpath "D:mongoDB4.0datadb" --port 27017 --serviceName "MongoDBService" --serviceDisplayName "MongoDB" --install
下表为mongodb启动的参数说明:
参数 描述
--bind_ip 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath 定MongoDB日志文件,注意是指定文件不是目录
--logappend 使用追加的方式写日志
--dbpath 指定数据库路径,注意:MongoDB默认使用db目录作为数据存储目录,但是这个目录需要手工创建,这里我们创建的是:D:mongoDB4.0datadb
--port 指定服务端口号,默认端口27017
--serviceName 指定服务名称
--serviceDisplayNam 指定服务名称,有多个mongodb服务时执行。
--install 指定作为一个Windows服务安装。
(4)启动服务:
启动:net start MongoDBService
停止:net stop MongoDBService
(5)MongoDB后台管理 Shell
mongo
退出:exit
参考:window平台安装 MongoDB
2、Linux安装
(1)官网下载:mongodb-linux-x86_64-4.0.12.tgz
(2)解压:
[root@node102 src]# tar -zxvf mongodb-linux-x86_64-4.0.12.tgz -C /usr/local
(3)安装:
首先创建数据目录和日志目录:
[root@node102 mongodb-linux-x86_64-4.0.12]# cd /usr/local/mongodb-linux-x86_64-4.0.12 && mkdir -p ./data/db ./log
注意:/data/db 是 MongoDB 默认的启动的数据库路径,如果你的数据库目录不是/data/db,可以通过 --dbpath 来指定。
然后执行启动命令:
[root@node102 mongodb-linux-x86_64-4.0.12]# cd ./bin && ./mongod --dbpath=/usr/local/mongodb-linux-x86_64-4.0.12/data/db --logpath=/usr/local/mongodb-linux-x86_64-4.0.12/log/mongodb.log --fork 2019-08-09T15:30:59.686+0800 I STORAGE [main] Max cache overflow file size custom option: 0 about to fork child process, waiting until server is ready for connections. forked process: 1492 child process started successfully, parent exiting
注意:
--fork:后台启动
(4)MongoDB后台管理 Shell,可以配置环境变量/etc/profile
[root@node102 bin]# ./mongo MongoDB shell version v4.0.12 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("dd068bb5-ddaf-4709-acb4-1f5e3bd6ff40") } MongoDB server version: 4.0.12 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2019-08-09T15:30:59.745+0800 I STORAGE [initandlisten] 2019-08-09T15:30:59.745+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-08-09T15:30:59.745+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-08-09T15:31:00.463+0800 I CONTROL [initandlisten] > 2+2 4 > exit bye [root@node102 bin]#
(5)关闭
(5.1)在mongo客户端关闭
> use admin
> db.shutdownServer()
(5.2)使用mongod,注意关闭哪个数据库 需要指定对应的--dbpath
[root@node102 bin]# ./mongod --dbpath=/usr/local/mongodb-linux-x86_64-4.0.12/data/db --shutdown
参考:Linux平台安装MongoDB
注意:mongoDB4好像不再支持web用户界面。
三、MongoDB存取原理
内部执行引擎为JS解释器(解释符合JS语法的语句):
插入操作:把JSON对象数据存储成BSON结构的文档结构,
查询操作:把BSON结构数据转换为JSON对象.
四、MongoDB和关系型数据库区别
参考:MongoDB 概念解析
1 package com.mongodb; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.bson.Document; 7 import org.bson.conversions.Bson; 8 import org.bson.types.ObjectId; 9 10 import com.mongodb.client.FindIterable; 11 import com.mongodb.client.MongoCollection; 12 import com.mongodb.client.MongoCursor; 13 import com.mongodb.client.MongoDatabase; 14 15 public class TestMongoDB { 16 17 public static void main(String[] args) 18 { 19 TestMongoDB test = new TestMongoDB(); 20 //插入数据 21 test.insertData(); 22 //查询数据 23 test.find(); 24 test.findone(); 25 //更新数据 26 test.update(); 27 //删除数据 28 test.delete(); 29 } 30 31 public void find() 32 { 33 //创建连接客户端 34 MongoClient client = new MongoClient("localhost",27017); 35 //获取数据库对象 36 MongoDatabase db = client.getDatabase("test"); 37 //获取操作的集合对象 38 MongoCollectioncollection = db.getCollection("wjy"); 39 //具体操作 40 FindIterable it = collection.find(); 41 //获取游标对象 42 MongoCursor cursor = it.iterator(); 43 while (cursor.hasNext()) 44 { 45 //取出每一个文档对象(行) 46 Document doc = cursor.next(); 47 String name = doc.getString("name"); 48 if ("乔峰".equals(name)) 49 { 50 Integer age = doc.getInteger("age"); 51 System.out.println(name+","+age); 52 } 53 else 54 { 55 Double age = doc.getDouble("age"); 56 System.out.println(name+","+age); 57 } 58 59 } 60 //释放资源 61 cursor.close(); 62 client.close(); 63 } 64 65 public void findone() 66 { 67 MongoClient client = new MongoClient("localhost",27017); 68 //获取数据库对象 69 MongoDatabase db = client.getDatabase("test"); 70 //获取操作的集合对象 71 MongoCollection collection = db.getCollection("wjy"); 72 //具体操作 73 Bson filter = new BasicDBObject("_id", new ObjectId("5850eacd7065f52b0eab7ff4")); 74 FindIterable it = collection.find(filter); 75 //获取游标对象 76 MongoCursor cursor = it.iterator(); 77 while (cursor.hasNext()) 78 { 79 //取出每一个文档对象(行) 80 Document doc = cursor.next(); 81 String name = doc.getString("name"); 82 if ("乔峰".equals(name)) 83 { 84 Integer age = doc.getInteger("age"); 85 System.out.println(name+","+age); 86 } 87 else 88 { 89 Double age = doc.getDouble("age"); 90 System.out.println(name+","+age); 91 } 92 93 } 94 //释放资源 95 cursor.close(); 96 client.close(); 97 } 98 99 public void insertData() 100 { 101 MongoClient client = new MongoClient("localhost",27017); 102 MongoDatabase db = client.getDatabase("test"); 103 MongoCollection collection = db.getCollection("wjy"); 104 //使用Map来封装json数据 105 Map map = new HashMap (); 106 map.put("name", "乔峰"); 107 map.put("age", 35); 108 map.put("gender", "true man"); 109 Map hobbyMap = new HashMap (); 110 hobbyMap.put("girl", "阿朱"); 111 hobbyMap.put("gongfu", "降龙十八掌"); 112 map.put("hobby", hobbyMap); 113 Document doc = new Document(map); 114 115 collection.insertOne(doc); 116 117 client.close(); 118 } 119 120 public void update() 121 { 122 MongoClient client = new MongoClient("localhost",27017); 123 MongoDatabase db = client.getDatabase("test"); 124 MongoCollection collection = db.getCollection("wjy"); 125 126 Bson filter = new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4")); 127 128 Map map = new HashMap (); 129 map.put("name", "张无忌"); 130 map.put("age", 35); 131 Bson update = new BasicDBObject(map); 132 133 collection.updateOne(filter, new BasicDBObject("$set",update)); 134 135 client.close(); 136 } 137 public void delete() 138 { 139 MongoClient client = new MongoClient("localhost",27017); 140 MongoDatabase db = client.getDatabase("test"); 141 MongoCollection collection = db.getCollection("wjy"); 142 143 collection.deleteOne(new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4"))); 144 145 client.close(); 146 } 147 }
参考:
官方网站:http://www.mongodb.org/
MongoDB教程:https://www.w3cschool.cn/mongodb/mongodb-intro.html