MongoDB是一个可扩展、高性能的分布式文档存储数据库,由C 语言编写,旨在为web应用提供可扩展的高性能数据存【本文来自鸿网互联 (http://www.68idc.cn)】储解决方案。它的特点是高性能、易部署、易使用,存储数据非常方便。 Mongo DB 是目前在IT行业非常流
MongoDB是一个可扩展、高性能的分布式文档存储数据库,由C 语言编写,旨在为web应用提供可扩展的高性能数据存【本文来自鸿网互联 (http://www.68idc.cn)】储解决方案。它的特点是高性能、易部署、易使用,存储数据非常方便。
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
文档数据库介绍:
MongoDB数据库中一条记录是一个文档,他的数据结构由(field)和值(value)成对的组成。MongoDB文档类似于JSON对象。字段(域)的值可以包含其他文档、数组和文档数组。
如下图所示MongoDB文档结构:

使用文档数据库的优势如下:
在许多程序设计语言中,文档(即对象)适合原生数据类型;
嵌入式文档和数组减少昂贵的关系型关联需要;
动态数据结构模式支持流畅的可扩展多态性。
安装
官方网站:http://www.mongodb.org/downloads,下载Windows 64bit地址。
MongoDB在Windows 7上的安装运行很方便。直接下载、解压,然后运行bin/mongod 即可启动服务器,运行bin/mongo 即可运行命令行客户端。
我是使用默认安装到C盘Program Files\MongoDB 2.6 Standard目录下,为了方便学习,将其拷贝到C盘根目录下,为C:\MongoDB。
注意:请最好不要安装到C盘Program Files目录下,而且安装目录不要包含空格,否则,将麻烦些,也就是命令行参数每个参数要用“”括起来,例如:
repeat "I am hungry" now
命令会把字符串"I am hungry"分配给argv[1],把字符串"now"分配给argv[2]。
在启动MongoDB之前,我们必须新建一个存放mongoDB数据和日志的目录。数据库目录:C:\MongoDB\data\db\,日志目录:C:\MongoDB\data\。
启动服务
打开CMD窗口,进入到C:\MongoDB\bin目录下,运行服务端mongod.exe。
C:\MongoDB\bin>mongod.exe --dbpath=C:\MongoDB\data\db --directoryperdb --logpath=C:\MongoDB\data\logs --logappend
注:如果服务未启动成功,主要是两个原因,一是未建data\db\目录;以及防火墙不允许开放服务所需端口。
运行客户端
再打开一个CMD窗口,进入到C:\MongoDB\bin目录下,运行客户端mongo.exe来登录MongoDB。(要保持服务端mongod.exe的窗口不关闭)
Java开发数据库驱动
驱动Jar包链接地址,驱动ZIP包链接地址。https://github.com/mongodb/mongo-java-driver/releases
在客户端练习
删除用户:db.dropUser('username')
创建OA数据库:use OA
注:如果不做其他操作,则OA数据库是不会被创建的。
创建collections:OA.createCollection("mytest");
查看数据库:
> show dbs
OA 0.078GB
admin 0.078GB
db (empty)
local 0.078GB
test (empty)
查看Collection(相当于“表”):
> show collections
创建文档数据数据表,并插入数据记录。
> use OA
switched to db OA
> db.createCollection("doctest")
{ "ok" : 1 }
> db.doctest.save({id:1,name:'ttest1'});
WriteResult({ "nInserted" : 1 })
> db.doctest.save({id:2,name:'ttest1',code:'102'});
WriteResult({ "nInserted" : 1 })
> db.doctest.save({id:3,name:'ttest3',code:'103',class:'doc'});
WriteResult({ "nInserted" : 1 })
> db.doctest.save({id:4,name:'ttest4',code:'104'});
WriteResult({ "nInserted" : 1 })
查询
查询数据数量(count)
> db.doctest.find().count();
4
条件(=)查询,条件为:name="ttest1"。
> db.doctest.find({"name":"ttest1"});
{ "_id" : ObjectId("54a1003556a081db9d632745"), "id" : 1, "name" : "ttest1" }
{ "_id" : ObjectId("54a1005756a081db9d632746"), "id" : 2, "name" : "ttest1", "code" : "102" }
条件(>=)查询,条件为:id>3。
> db.doctest.find({id:{$gt:3}});
{ "_id" : ObjectId("54a100a056a081db9d632748"), "id" : 4, "name" : "ttest4", "code" : "104" }
> db.doctest.find({id:{$gte:3}});
{ "_id" : ObjectId("54a1008c56a081db9d632747"), "id" : 3, "name" : "ttest3", "code" : "103", "class" : "doc" }
{ "_id" : ObjectId("54a100a056a081db9d632748"), "id" : 4, "name" : "ttest4", "code" : "104" }
条件(in)查询,条件为:id in (2,3)。
> db.doctest.find({id:{$in:[2,3]}});
说明:$gt : > --(Greater than 的首字母)
$gte : >= --(Greater than or equal 的首字母)
$lt :
$lte :
$ne : != --(Not equal 的首字母)
推荐客户端工具
1. MongoVUE ,http://blog.mongovue.com/
数据库设计
MongoDB 无固定结构,每张表每段数据可以有不同的结构,这既是好处也是缺点,缺点在于你必须很了解MongoDB的表结构,这其实给维护人员带来一定的不适应和麻烦。
此问题也很容易解决,就是增加表结构定义表,用于说明各种情况下的结构定义,例如可配置审批单,就是比较适用。
嵌套式设计,例如审批单带有明细项目,其中,明细项目是多行数据内容,则操作如下:
>db.doctest.save({id:5,name:'ttest5',code:'106',detail:[{item:'测试卡1',type:'Card',acount:3},{item:'测试卡2',type:'Card',acount:5}]});
查询其中型号(Type)是“Mobile”的记录,在操作如下:
> db.doctest.find({"detail.type":"Mobile"});
{ "_id" : ObjectId("54a39ebdd8389293ac59e78a"), "id" : 6, "name" : "ttest6", "code" : "107", "detail" : [ { "item" : "员工卡1", "type" : "Card", "acount" : 3 }, { "item" : "测试手机", "type" : "Mobile", "acount" : 5 } ] }
查看审批单的设计:
> db.doctest.find();
查询内嵌文档
查询文档有两种方式,一种是完全匹查询,另一种是针对键值对查询!内嵌文档的完全匹配查询和数组的完全匹配查询一样,内嵌文档内键值对的数量,顺序都必须一致才会匹配,如下例:
针对内嵌文档特定键值对的查询是最常用的!通过点表示法来精确表示内嵌文档的键。

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

InnoDB is suitable for applications that require transaction support and high concurrency, while MyISAM is suitable for applications that require more reads and less writes. 1.InnoDB supports transaction and bank-level locks, suitable for e-commerce and banking systems. 2.MyISAM provides fast read and indexing, suitable for blogging and content management systems.

There are four main JOIN types in MySQL: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN returns all rows in the two tables that meet the JOIN conditions. 2.LEFTJOIN returns all rows in the left table, even if there are no matching rows in the right table. 3. RIGHTJOIN is contrary to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet JOIN conditions.

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
