当前位置: 首页 > 编程日记 > 正文

【MongoDB学习之一】初始MongoDB

环境
  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.0\bin,启动cmd:

mongod --bind_ip 127.0.0.1 --logpath "D:\mongoDB4.0\log\mongodb.log" --logappend --dbpath "D:\mongoDB4.0\data\db" --port 27017 --serviceName "MongoDBService" --serviceDisplayName "MongoDB" --install

下表为mongodb启动的参数说明:

参数 描述
--bind_ip 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath 定MongoDB日志文件,注意是指定文件不是目录
--logappend 使用追加的方式写日志
--dbpath 指定数据库路径,注意:MongoDB默认使用db目录作为数据存储目录,但是这个目录需要手工创建,这里我们创建的是:D:\mongoDB4.0\data\db
--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 <address> 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                 MongoCollection<Document> collection = db.getCollection("wjy");
 39                 //具体操作
 40                 FindIterable<Document> it = collection.find();
 41                 //获取游标对象
 42                 MongoCursor<Document> 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<Document> collection = db.getCollection("wjy");
 72         //具体操作
 73         Bson filter = new BasicDBObject("_id", new ObjectId("5850eacd7065f52b0eab7ff4"));
 74         FindIterable<Document> it = collection.find(filter);
 75         //获取游标对象
 76         MongoCursor<Document> 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<Document> collection = db.getCollection("wjy");
104         //使用Map来封装json数据
105         Map<String,Object> map = new HashMap<String,Object>();
106         map.put("name", "乔峰");
107         map.put("age", 35);
108         map.put("gender", "true man");
109         Map<String,Object> hobbyMap = new HashMap<String,Object>();
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<Document> collection = db.getCollection("wjy");
125         
126         Bson filter = new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4"));
127         
128         Map<String,Object> map = new HashMap<String,Object>();
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<Document> 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

转载于:https://www.cnblogs.com/cac2020/p/6184734.html

相关文章:

swift 和 oc中检测textfield是否输入数字

iOS 开发中用来检测输入框是否输入的是纯数字 Swift 版本 // 代理方法func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {//判断输入的是否是数字 否则无效var cs CharacterSet();// 获取除…

开源项目贡献者_如何吸引新的贡献者加入您的开源项目

开源项目贡献者by Shubheksha通过Shubheksha 如何吸引新的贡献者加入您的开源项目 (How to attract new contributors to your open source project) It’s hard to attract contributors to your FOSS project — especially contributors who are new to open source.很难吸…

滑动轮播图实现最后一张图片无缝衔接第一张图片

原理&#xff1a;使用insertBefore和insertAfter方法调整图片顺序。 测试&#xff1a;firefox/chrome/IE11正常 已知不足&#xff1a;每次播放均使用了一次insertBefore和insertAfter&#xff0c;可考虑在最后一张图的时候将前几张图片整体后移。以后有空再优化。 1、HTML结构 …

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一...

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一 原文:一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一上一篇&#xff1a;一个完整的安装程序实例—艾泽拉斯之海…

数据结构,堆和栈和队列的概念

数据结构&#xff0c;堆和栈和队列的概念 1 什么是数据结构 数据结构是计算机存储&#xff0c;组织数据的反复改。数据结构是指相互之间存在的一种或多种特定关系的数据元素集合。 2 数据结构的逻辑结构 1 集合结构&#xff0c;元素都是孤立存在的 2 线性结构 &#xff0c;…

电子白板 矢量 编码_当涉及白板编码采访时,请记住准备

电子白板 矢量 编码by Andy Tiffany通过安迪蒂芙尼(Andy Tiffany) 当涉及白板编码采访时&#xff0c;请记住准备 (When it comes to whiteboard coding interviews, remember to PREP) PREP is a mnemonic I created to help you remember the steps involved in solving whit…

机器学习实战笔记(Python实现)-03-朴素贝叶斯

--------------------------------------------------------------------------------------- 本系列文章为《机器学习实战》学习笔记&#xff0c;内容整理自书本&#xff0c;网络以及自己的理解&#xff0c;如有错误欢迎指正。 源码在Python3.5上测试均通过&#xff0c;代码及…

SQLite.swift的简单使用

使用cocoapod 来进行引入 pod ‘SQLite.swift’ // // SQLiteTool.swift // CreateLectureForSwift // // Created by coder on 2019/6/25. // Copyright © 2019 AlexanderYeah. All rights reserved. // import Foundation import SQLite // id let id Expression(“…

Cypress USB开发文档列表(积累中)

CyUSB.chm(pdf) \Cypress\Cypress Suite USB 3.4.7\Driver Cypress CyUSB.sys Programmers Reference 内容: CyUsb.sys、CyUsb.inf 驱动程序介绍&#xff0c;如何绑定设备到驱动程序&#xff0c;以IOCTL Interface、CYIOCTL.Hd的解释为主要内容的编程指导&#xff0c;主…

对象冒充_使用您的精神探照灯进行冒充冒名顶替综合症

对象冒充by Jaime J. Rios由Jaime J. Rios “Stop that imposter! Seize them!”“停止冒名顶替者&#xff01; 抓住他们&#xff01;” I first spotted my imposter two years ago. It happened when I began learning how to code.两年前&#xff0c;我第一次发现了冒名顶…

grep 函数

linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具&#xff0c;它能使用正则表达式搜索文本&#xff0c;并把匹 配的行打印出来。grep全称是Global Regular Expression Print&#xff0c;表示全局正则表达式版本&#xff0c;它的使用权限是所有用户。 2.格式gr…

iOS weak 自动置为nil的实现

1 weak 自动置为nil的实现 runtime 维护了一个Weak表&#xff0c;weak_table_t 用于存储指向某一个对象的所有Weak指针。Weak表其实是一个哈希表&#xff0c; key是所指对象的地址&#xff0c;value是weak指针的地址的数组。 在对象回收的时候&#xff0c;就会在weak表中进…

iOS 缓存策略

Github https://github.com/gaosboy/kache https://github.com/swtlovewtt/WTRequestCenter https://github.com/hans007/CacheFile Image https://github.com/youger/UIImageView-ASIImageCache转载于:https://www.cnblogs.com/hl666/p/3931182.html

数据结构的简要介绍:图形如何工作

by Michael Olorunnisola通过Michael Olorunnisola 数据结构的简要介绍&#xff1a;图形如何工作 (A Gentle Introduction to Data Structures: How Graphs Work) So who wants to work at Google, Facebook, or maybe LinkedIn? Beyond their grueling interview process, o…

Catel(翻译)-为什么选择Catel

1. 介绍 这篇文章主要是为了说明&#xff0c;我们为什么要使用Catel框架作为开发WPF&#xff0c;Silverlight,和Windows phone7应用程序的开发框架。 2. 通用功能 2.1. 这是你的选择 针对需对开发者&#xff0c;再使用架构的时候是希望有很大的自由度的&#xff0c;但是大部…

iOS 三种类型的Block

Block 的copy 操作 Block 其实来讲有三种类型 全局块 NSConcreteGlobalBlock 栈块 NSConcreteStackBlock 堆块 NSConcreteMallocBlock 全局块存储在全局内存中&#xff0c;相当于单例 栈块存于栈内存中&#xff0c;超出其作用域则马上进行销毁 堆块存在于堆内存中&#x…

2.4G高频PCB天线设计

2.4G高频PCB天线设计转载于:https://www.cnblogs.com/LittleTiger/p/6215262.html

如何一起破解图形化Python调试器

15分钟内从零调试 (Zero-to-Debugging in 15 mins) You don’t realize the value of a debugger until you’re stuck working on a hard-to-visualize problem. But once you fire up a development environment with decent debugging capabilities, you’ll never look bac…

python 之路,Day11 (下)- sqlalchemy ORM

python 之路&#xff0c;Day11 - sqlalchemy ORM 本节内容 ORM介绍sqlalchemy安装sqlalchemy基本使用多外键关联多对多关系表结构设计作业1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序&#xff0c;简单来说我们类似python这种面向对象的程序来说一切…

iOS事件响应链

1 如下 NSObject 显然是基类&#xff0c;都是继承与UIResponder. 可以看出UIApplication&#xff0c;UIView&#xff0c;UIViewController都是继承自UIResponder类&#xff0c;可以响应和处理事件 我们都是通过UIResonder 来查找控件的父视图控件。’ 发生触摸事件之后&…

论5级流水32bit risc cpu设计

前段时间用verilog写了一个32bit的risc cpu,五级流水&#xff0c;下板调试已经完全可用&#xff0c;准备后期加入浮点运算器&#xff0c;因为最近事情超级多&#xff0c;因此暂时先把RTL图传上来供大家参考&#xff0c;后面我会讲具体怎么设计。希望大家多多关注 :)转载于:http…

开源项目贡献者_嘿新手开源贡献者:请写博客。

开源项目贡献者by Shubheksha通过Shubheksha 嘿新手开源贡献者&#xff1a;请写博客。 (Hey newbie open source contributors: please blog more.) As a newbie open source contributor, I often felt lost and dejected. I couldn’t figure out how different modules fit…

instanceof, isinstance,isAssignableFrom的区别

instanceof运算符 只被用于对象引用变量&#xff0c;检查左边的被测试对象 是不是 右边类或接口的 实例化。如果被测对象是null值&#xff0c;则测试结果总是false。 形象地&#xff1a;自身实例或子类实例 instanceof 自身类 返回true 例&#xff1a; String snew String(&qu…

POJ - 3538 - Domestic Networks

先上题目&#xff1a; Domestic NetworksTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 732 Accepted: 204 Special JudgeDescription Alex is a system administrator of Domestic Networks Inc. His network connects apartments and spans over multiple buil…

iOS HitTest 机制

当用户触摸&#xff08;Touch&#xff09;屏幕进行交互时&#xff0c;系统首先要找到响应者&#xff08;Responder&#xff09;。系统检测到手指触摸&#xff08;Touch&#xff09;操作的时候&#xff0c;将Touch 以UIEvent 的方式加入到UIApplication 事件队列中去。UIApplica…

巨石加密_缓解巨石

巨石加密by Ian Belcher伊恩贝尔彻(Ian Belcher) 我们如何将技术堆栈转向基于服务&#xff0c;以开发人员体验为中心的设计 (How we pivoted our tech stack to a service-based, developer experience-focused design) This article documents the problems we experienced w…

Python函数中的参数(一)

函数传递参数时的简要关键点&#xff1a; 1、参数的传递是通过自动将对象赋值给本地变量名来实现的。函数参数在实际中只是Python赋值的一个实例。因为引用是以指针的形式实现的&#xff0c;所有的参数实际上都是通过指针进行传递的。 2、在函数内部的参数名的赋值不会影响调用…

LLDB 调试相关

LLDB 初始 LLDB 是一个有着 REPL 的特性和 C ,Python 插件的开源调试器。LLDB 绑定在 Xcode 内部&#xff0c;存在于主窗口底部的控制台中。调试器允许你在程序运行的特定时暂停它&#xff0c;你可以查看变量的值&#xff0c;执行自定的指令&#xff0c;并且按照你所认为合适的…

javascript优缺点_为什么要在JavaScript中使用静态类型? 优缺点

javascript优缺点by Preethi Kasireddy通过Preethi Kasireddy 为什么要在JavaScript中使用静态类型&#xff1f; 优缺点 (Why use static types in JavaScript? The Advantages and Disadvantages) We covered a lot of ground in Part 1! With syntax out of the way, let’…

大数的减法函数--c语言

代码展示&#xff1a; http://paste.ubuntu.com/23693598/ #include<stdio.h> #include<stdlib.h> #include<string.h> char * largeDiffer(char *a,char *b){ /* 使用说明 传入的a和b只能为整数 结果为a-b;返回的为字符指针&#xff0c;注意数组不要越…