MongoDB讲义
- 格式:ppt
- 大小:370.00 KB
- 文档页数:57
mongodb教程MongoDB教程MongoDB是一种开源的、面向文档的NoSQL数据库管理系统。
它以高性能、高可扩展性和易用性而闻名,是当今最受欢迎的NoSQL数据库之一。
本文将介绍MongoDB的基本概念、使用方法和常见操作等内容,帮助读者快速上手和应用MongoDB。
1. MongoDB简介MongoDB是由MongoDB公司开发和维护的一个开源项目。
它是一个面向文档的数据库,数据以BSON(Binary JSON)格式存储,支持丰富的数据结构,包括文档、数组、嵌套文档等。
MongoDB采用基于分布式文件存储的方式,支持水平扩展和高可用性。
它适用于处理海量数据和高并发访问的场景。
2. 安装和配置MongoDB要使用MongoDB,首先需要安装和配置它。
MongoDB提供了针对多个操作系统的安装包和二进制文件,安装过程相对简单。
安装完成后,可以通过编辑配置文件来配置MongoDB的参数,例如端口号、数据库存储路径等。
默认情况下,MongoDB会将数据存储在/var/lib/mongodb目录下。
3. 连接和运行MongoDB安装和配置完成后,可以使用命令行或图形界面工具连接和运行MongoDB。
MongoDB提供了丰富的命令行工具,例如mongo、mongodump、mongorestore等,它们可以用于数据库的管理和操作。
此外,还有许多第三方的可视化工具,例如Robo 3T、MongoDB Compass等,它们提供了更友好的界面和更多的功能。
4. 数据库和集合的操作在MongoDB中,数据库是用于存储数据的容器,而集合则是一组相关的文档。
可以使用命令行或图形界面工具创建数据库和集合,并对其进行增删改查等操作。
例如,使用db.createCollection()命令可以创建一个新的集合,使用db.collection.insertOne()和db.collection.insertMany()命令可以插入一个或多个文档,使用db.collection.find()命令可以查询文档。
mongodb 基础教学-回复MongoDB 是一种流行的NoSQL 数据库,它是开源的,使用JSON 风格的文档进行数据存储。
它的设计目标是供大规模、高性能的数据存储和处理需求使用。
本文将介绍MongoDB 的基本概念和使用方法,帮助读者理解并开始使用MongoDB。
1. 什么是MongoDB?MongoDB 是一个面向文档的数据库管理系统。
与传统的关系型数据库不同,MongoDB 使用文档代替了行和列,文档以JSON 或BSON 的形式存储。
MongoDB 以文档为单位进行数据存储和查询,文档可以是不同结构和属性的,这使得MongoDB 在处理半结构化数据非常灵活和高效。
2. 安装和配置MongoDB首先,我们需要下载并安装MongoDB 软件包。
根据所使用的操作系统,可以在MongoDB 官方网站找到对应的安装包,并按照安装指南进行安装。
安装完成后,需要进行一些配置。
首先,创建一个空文件夹作为MongoDB 的数据存储位置。
然后,使用配置文件指定数据目录和其他参数:storage:dbPath: /path/to/data/dbsystemLog:destination: filepath: /path/to/mongodb.lognet:bindIp: 127.0.0.1配置完成后,启动MongoDB 服务器。
3. 数据库和集合MongoDB 中的数据存储在数据库中,数据库由集合组成,而集合则由文档组成。
可以将数据库理解为关系型数据库中的数据库,集合类似于表,文档类似于表中的行。
在MongoDB 中,可以使用以下命令来创建数据库和集合:use mydb 创建名为mydb 的数据库db.createCollection("mycollection") 在mydb 数据库中创建名为mycollection 的集合4. 插入文档插入文档是向MongoDB 中添加数据的常用操作。
mongodb讲解
MongoDB是一种基于文档的数据库管理系统,以非关系型数据库
技术为基础。
它已成为当今最流行的NOSQL数据库之一,广泛用于各种Web应用程序、电子商务和社交媒体。
以下是MongoDB的一些重要的特点和优势。
1. 非关系型:相对于传统的关系型数据库,MongoDB以文档的方式存
储数据,使得数据的结构更灵活,更能适应复杂多变的数据需求。
2. 非结构化数据:MongoDB可以处理非结构化数据,例如图像、音频、视频等等。
3. 扩展性:MongoDB有很好的扩展性,可以通过水平扩展增加服务器,而不用像传统关系型数据库那样需要增加硬件来实现扩展。
4. 高性能:MongoDB能够进行高效的数据读写操作,因为它能够快速
处理复杂的查询操作。
5. 数据可靠性:MongoDB提供了数据的副本备份、故障恢复等机制,
可以保证数据的可靠性和安全性。
6. 易于使用:MongoDB可以使用各种编程语言进行开发,包括Java、Python、PHP等等,相关的驱动程序和库也很齐全。
7. 数据库设计:MongoDB支持丰富的数据建模特性,可以进行更灵活、自然的数据建模。
总之,MongoDB的使用受到越来越多企业和开发者的认可和喜爱,其
高性能、扩展性、可靠性和易用性,成为该数据库领域的佼佼者。
如
果你需要一种可扩展、易于部署而且运行快速的数据库技术,MongoDB无疑是一种值得推荐的选择。
mongodb复习资料MongoDB复习资料MongoDB是一种流行的NoSQL数据库,被广泛应用于各种大规模数据管理和处理场景。
本文将为读者提供一份MongoDB复习资料,帮助大家回顾和巩固相关知识。
一、MongoDB简介MongoDB是一个开源的文档数据库,采用了面向文档的数据模型。
它以BSON (Binary JSON)格式存储数据,支持动态模式和灵活的查询语言。
相比传统的关系型数据库,MongoDB在处理大量数据和高并发访问时表现更为出色。
二、数据模型MongoDB的数据模型是以文档为基本单位的。
文档是一个键值对的集合,类似于关系型数据库中的一行数据。
文档可以包含各种类型的值,如字符串、整数、数组、嵌套文档等。
通过使用文档,MongoDB可以轻松地表示复杂的数据结构。
三、集合与文档MongoDB中的数据存储在集合(Collection)中,一个集合可以包含多个文档。
集合类似于关系型数据库中的表,文档则类似于表中的行。
通过使用集合和文档,MongoDB可以高效地存储和查询数据。
四、索引索引是MongoDB中提高查询性能的重要机制。
MongoDB支持各种类型的索引,如单字段索引、复合索引、文本索引等。
通过创建适当的索引,可以加快查询速度并提高系统性能。
五、查询语言MongoDB的查询语言非常灵活,支持各种类型的查询操作。
常见的查询操作包括查找、排序、过滤、投影等。
通过熟练掌握查询语言,可以高效地检索和处理数据。
六、聚合框架MongoDB提供了强大的聚合框架,用于处理和分析大规模数据集。
聚合框架支持各种聚合操作,如分组、排序、筛选、计数等。
通过使用聚合框架,可以进行复杂的数据处理和统计分析。
七、事务处理MongoDB从版本4.0开始支持事务处理。
事务是一组操作的逻辑单元,要么全部执行成功,要么全部回滚。
通过使用事务,可以确保数据的一致性和完整性。
八、安全性MongoDB提供了多种安全机制,保护数据免受未经授权的访问和恶意攻击。
Mongodb数据库基础入门(一)Mongodb介绍Mongodb是一个基于分布式文件存储的数据库,由C 语言编写,为WEB应用提供可扩展的高性能数据存储解决方案Mongodb是一款介于关系型数据库与非关系型数据库之间的产品, Mongodb是不同于以往的如redis、memcached,它是一种叫文档数据库,存储的是文档(bson-->json的二进制化)特点:最大的特点是支持查询语言非常强大,内部执行的引擎是JS解释器,把文档存储成bson结构,查询时将文档转换成JS对象文件,并通过熟悉JS语法来操作同传统数据库比较:1、传统数据库是结构化数据,有表结构,每一行内容是符合表结构,且列的类型也一样2、mongodb数据库是以文档形式存储数据,每一个文档都是有自己独特的结构(js对象)与属性、值,因此它没有特定的规范与格式Mongodb安装官方网站:下载最新的stable版本[root@mingongge ~]# cd /usr/local/src/[root@mingongge src]# wget /dr/fastdl./linux/mongodb-linux-x86_64-rhel62-3.4.6.tgz[root@Centos-2 src]# tar zxf mongodb-linux-x86_64-rhel62-3.4.6.tgz[root@Centos-2 src]# cd mongodb-linux-x86_64-rhel62-3.4.6[root@Centos-2 mongodb-linux-x86_64-rhel62-3.4.6]# cd bin/[root@Centos-2 bin]# lltotal 277780-rwxr-xr-x 1 root root 10431547 Jul 6 02:23 bsondump#二进制导出(bson结构)-rwxr-xr-x 1 root root 29870496 Jul 6 02:48 mongo#客户端-rwxr-xr-x 1 root root 54389424 Jul 6 02:48 mongod#服务端-rwxr-xr-x 1 root root 12771652 Jul 6 02:24 mongodump #导出数据库-rwxr-xr-x 1 root root 10783691 Jul 6 02:23 mongoexport #导出易识别的json文档或CSV-rwxr-xr-x 1 root root 10668482 Jul 6 02:23 mongofiles-rwxr-xr-x 1 root root 10942731 Jul 6 02:23 mongoimport -rwxr-xr-x 1 root root 10433507 Jul 6 02:24 mongooplog-rwxr-xr-x 1 root root 53753432 Jul 6 02:48 mongoperf-rwxr-xr-x 1 root root 14070941 Jul 6 02:24 mongoreplay-rwxr-xr-x 1 root root 14127528 Jul 6 02:24 mongorestore #导入数据库-rwxr-xr-x 1 root root 30539024 Jul 6 02:48 mongos#路由器(分片)-rwxr-xr-x 1 root root 11003296 Jul 6 02:23 mongostat#状态-rwxr-xr-x 1 root root 10631445 Jul 6 02:24 mongotop[root@Centos-2 src]# mv mongodb-linux-x86_64-rhel62-3.4.6 /usr/local/mongodb[root@Centos-2 src]# cd /usr/local/mongodb/启动服务创建数据目录与日志目录[root@Centos-2 ~]# mkdir /data/mongodb -p[root@Centos-2 ~]# mkdir /data/mongodblog -p[root@Centos-2 mongodb]# ./bin/mongod --dbpath /data/mongodb/ --logpath /data/mongodblog/mongo.log --fork --port 27017about to fork child process, waiting until server is ready for connections.forked process: 19027child process started successfully, parent exiting[root@Centos-2 mongodb]# lsof -i :27017COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmongod 19027 root 7u IPv4 48419 0t0 TCP *:27017 (LISTEN) 参数说明:--dbpath 指定数据存储目录--logpath 指定日志存储目录--fork 后台运行--port 指定端口(默认27017)连接数据库[root@Centos-2 mongodb]# ./bin/mongoMongoDB shell version v3.4.6connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.6Server has startup warnings:2017-07-29T10:36:50.683 0800 I STORAGE [initandlisten]2017-07-29T10:36:50.683 0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine2017-07-29T10:36:50.683 0800 I STORAGE [initandlisten] ** See http://dochub./core/prodnotes-filesystem2017-07-29T10:36:51.494 0800 I CONTROL [initandlisten] 2017-07-29T10:36:51.494 0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.2017-07-29T10:36:51.494 0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.2017-07-29T10:36:51.494 0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2017-07-29T10:36:51.494 0800 I CONTROL [initandlisten] 2017-07-29T10:36:51.495 0800 I CONTROL [initandlisten] 2017-07-29T10:36:51.495 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2017-07-29T10:36:51.495 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2017-07-29T10:36:51.495 0800 I CONTROL [initandlisten] 2017-07-29T10:36:51.495 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2017-07-29T10:36:51.496 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2017-07-29T10:36:51.496 0800 I CONTROL [initandlisten] 2017-07-29T10:36:51.496 0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7671 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.2017-07-29T10:36:51.496 0800 I CONTROL [initandlisten]报错解决方法如下:WARNING: Using the XFS filesystem is strongly recommend ed with the WiredTiger storage engine强烈建议使用带WiredTiger存储引擎的XFS文件系统WARNING:Access control is not enabled for the database.意思是:未对数据库启用访问控制,对数据和配置的读写访问不受限制解决方法:开启数据库的认证就可以解决在配置文件mongod.conf中开启,如下:security:authorization: enabledWARNING:/sys/kernel/mm/transparent_hugepage/enabled is 'always'.#cat /sys/kernel/mm/transparent_hugepage/enabled[always] madvise never关闭命令:#echo never > /sys/kernel/mm/transparent_hugepage/enab ledWARNING:/sys/kernel/mm/transparent_hugepage/defrag is 'always'.将/sys/kernel/mm/transparent_hugepage/defrag设置为never#cat /sys/kernel/mm/transparent_hugepage/defrag[always] madvise never关闭命令:echo never >/sys/kernel/mm/transparent_hugepage/defragWARNING:soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 设置ulimitvi /etc/security/limits.confmongod soft nofile 64000mongod hard nofile 64000mongod soft nproc 32000mongod hard nproc 32000重启mongodb服务后重新登陆[root@Centos-2 mongodb]# ./bin/mongoMongoDB shell version v3.4.6connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.6> show databases;admin 0.000GBlocal 0.000GB> show dbs; #查看当前的数据库admin 0.000GB #管理数据库local 0.000GB来源:。
快速掌握mongoDB(三)——mongoDB的索引详解1 mongoDB索引的管理 本节介绍mongoDB中的索引,熟悉mysql/sqlserver等关系型数据库的⼩伙伴应该都知道索引对优化数据查询的重要性。
我们先简单了解⼀下索引:索引的本质就是⼀个排序的列表,在这个列表中存储着索引的值和包含这个值的数据(数据row或者document)的物理地址,索引可以⼤⼤加快查询的速度,这是因为使⽤索引后可以不再扫描全表来定位某⾏的数据,⽽是先通过索引表找到该⾏数据对应的物理地址(多数为B-tree查找),然后通过地址来访问相应的数据。
索引可以加快数据检索、排序、分组的速度,减少磁盘I/O,但是索引也不是越多越好,因为索引本⾝也是数据表,需要占⽤存储空间,同时索引需要数据库进⾏维护,当我们对索引列的值进⾏增改删操作时,数据库需要更新索引表,这会增加数据库的压⼒。
我们要根据实际情况来判断哪些列适合添加索引,哪些列不适合添加索引,⼀般遵循的规律如下: 主/外键列,主键⽤于强制该列的唯⼀性和组织表中数据的排列结构;外键可以加快连接的速度; 经常⽤于⽐较的类(⼤于⼩于等于等),因为索引已经排序,值就是⼤于/⼩于的分界点; 经常进⾏范围搜索,因为索引已经排序,其指定的范围是连续的; 经常进⾏排序的列,因为索引已经排序,这样查询可以利⽤索引的排序,加快排序查询时间; 经常进⾏分组的列,因为索引已经排序,同⼀个值的所有数据地址会聚集在⼀块,很⽅便分组。
我们看⼀下mongoDB的索引使⽤,⾸先准备数据:erinfos.insertMany([{_id:1, name: "张三", age: 23,level:10, ename: { firstname: "san", lastname: "zhang"}, roles: ["vip","gen" ]},{_id:2, name: "李四", age: 24,level:20, ename: { firstname: "si", lastname: "li"}, roles:[ "vip" ]},{_id:3, name: "王五", age: 25,level:30, ename: { firstname: "wu", lastname: "wang"}, roles: ["gen","vip" ]},{_id:4, name: "赵六", age: 26,level:40, ename: { firstname: "liu", lastname: "zhao"}, roles: ["gen"] },{_id:5, name: "⽥七", age: 27, ename: { firstname: "qi", lastname: "tian"}, address:'北京' },{_id:6, name: "周⼋", age: 28,roles:["gen"], address:'上海' }]); 索引的增删改查还是⼗分简单的,我们看⼀下索引管理的⼏个⽅法://创建索引,值1表⽰正序排序,-1表⽰倒序排序 erinfos.createIndex({age:-1})//查看userinfos中的所有索引 erinfos.getIndexes()//删除特定⼀个索引 erinfos.dropIndex({name:1,age:-1})//删除所有的索引(主键索引_id不会被删除) erinfos.dropIndexes()//如果我们要修改⼀个索引的话,可以先删除索引然后在重新添加。
MongoDB数据库技术入门MongoDB是一款非关系型数据库,采用类似于Javascript的BSON格式存储数据,广泛应用于Web、移动应用、物联网等领域。
本文将为大家介绍MongoDB的基本概念和使用方法。
一、MongoDB概述MongoDB由10gen公司于2007年创建,目标是提供一种可扩展的高性能、易使用、可靠的数据库系统。
与传统关系型数据库相比,MongoDB具有更好的可扩展性和更加灵活的数据模型。
它支持丰富的查询语言,并具有高效的读写速度,在大数据处理方面有着很好的表现。
MongoDB的特点有:1. 高可扩展性:支持集群分布式部署,支持数据分片和自动负载均衡。
2. 易用性:MongoDB采用类似于Javascript的BSON格式存储数据,具有很好的可读性和易用性。
3. 数据模型灵活:MongoDB没有固定的模式,数据结构不需要使用预先定义的模式,可提供更加灵活的数据模型。
4. 高性能:MongoDB支持并发读写操作和索引,读取速度快。
5. 支持丰富的查询语言:MongoDB支持丰富的查询语言,包括聚合查询、地理空间查询、文本搜索等。
二、 MongoDB的安装MongoDB的安装非常简单,只需下载相应的安装包即可。
安装完毕后,在命令行界面键入mongo,即可进入MongoDB的交互式命令行环境。
安装完毕后,需要配置环境变量。
在Windows环境下,可将mongod.exe所在路径加入PATH中,使得mongod命令可直接在命令行中使用。
三、 MongoDB的基本操作1. 创建数据库和集合MongoDB中的数据都存储在集合(collection)中,集合相当于关系型数据库中的表。
要创建一个新的数据库和集合,只需在交互式命令行环境中输入:> use mydb> db.createCollection("user")这里,use mydb用于创建名为mydb的数据库,而db.createCollection("user")则用于创建名为user的集合。
About the T utorialMongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++.This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.AudienceThis tutorial is designed for Software Professionals who are willing to learn MongoDB Database in simple and easy steps. It will throw light on MongoDB concepts and after completing this tutorial you will be at an intermediate level of expertise, from where you can take yourself at higher level of expertise.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of database, text editor and execution of programs, etc. Because we are going to develop high performance database, so it will be good if you have an understanding on the basic concepts of Database (RDBMS).Copyright & DisclaimerCopyright 2018 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents (ii)MONGODB (1)1.MongoDB ─ Overview (2)2.Mongo DB ─ Advantages (4)3.MongoDB ─ Environment (5)4.MongoDB ─ Data Modelling (10)5.MongoDB ─ Create Database (12)6.MongoDB ─ Drop Database (13)7.MongoDB ─ Create Collection (14)8.MongoDB ─ Drop Collection (16)9.MongoDB ─ Datatypes (17)10.MongoDB ─ Insert Document (18)11.MongoDB ─ Query Document (20)12.MongoDB ─ Update Document (24)13.MongoDB ─ Delete Document (26)14.MongoDB ─ Projection (28)15.MongoDB ─ Limit Records (29)16.MongoDB ─ Sort Records (31)17.MongoDB ─ Indexing (32)18.Mo ngoDB ─ Aggregation (34)19.MongoDB ─ Replication (38)20.MongoDB ─ Sharding (41)21.Mo ngoDB ─ Create Backup (43)22.MongoDB ─ Deployment (45)23.MongoDB ─ Java (48)24.MongoD B ─ PHP (60)ADVANCED MONGODB (66)25.MongoDB ─ Relationships (67)26.MongoDB ─ Database References (70)27.MongoDB ─ Covered Queries (72)28.MongoDB ─ Analyzing Queries (74)29.MongoDB ─ Atomic Operations (76)30.MongoDB ─ Advanced Indexing (78)31.MongoDB ─ Indexing Limitations (80)32.MongoDB ─ ObjectId (81)33.MongoDB ─ MapReduce (83)34.MongoDB ─ Text Search (86)35.MongoDB ─ Regular Expression (88)36.MongoDB ─ RockMongo (90)37.MongoDB ─ GridFS (91)38.Mong oDB ─ Capped Collections (93)39.MongoDB ─ Auto-Increment Sequence (95)MongoDB4MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.DatabaseDatabase is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.CollectionCollection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.DocumentA document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data. The following table shows the relationship of RDBMS terminology with MongoDB.5MongoDB6Sample DocumentFollowing example shows the document structure of a blog site, which is simply a comma separated key value pair._id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you don’t provide then MongoDB provides a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB server and remaining 3 bytes are simple incremental VALUE.Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB, there is no concept of relationship.Advantages of MongoDB over RDBMS∙Schema less: MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.∙Structure of a single object is clear.∙No complex joins.∙Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.∙Tuning.∙Ease of scale-out: MongoDB is easy to scale.∙Conversion/mapping of application objects to database objects not needed.∙Uses internal memory for storing the (windowed) working set, enabling faster access of data.Why Use MongoDB?∙Document Oriented Storage: Data is stored in the form of JSON style documents.∙Index on any attribute∙Replication and high availability∙Auto-sharding∙Rich queries∙Fast in-place updates∙Professional support by MongoDBWhere to Use MongoDB?∙Big Data∙Content Management and Delivery∙Mobile and Social Infrastructure7∙User Data Management∙Data Hub89Let us now see how to install MongoDB on Windows.Install MongoDB on WindowsTo install MongoDB on Windows, first download the latest release of MongoDB from /downloads . Make sure you get correct version of MongoDB depending upon your Windows version. To get your Windows version, open command prompt and execute the following command. 32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.Now extract your downloaded file to c:\ drive or any other location. Make sure the name of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Here [version] is the version of MongoDB download.Next, open the command prompt and run the following command. In case you have extracted the MongoDB at different location, then go to that path by using command cd FOOLDER/DIR and now run the above given process.MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt. Execute the following command sequence. If you have to install the MongoDB at a different location, then you need to specify an alternate path for \data\db by setting the path dbpath in mongod.exe . For the same, issue the following commands.10In the command prompt, navigate to the bin directory present in the MongoDB installation folder. Suppose my installation folder is D:\set up\mongodbThis will show waiting for connections message on the console output, which indicates that the mongod.exe process is running successfully.Now to run the MongoDB, you need to open another command prompt and issue the following command.This will show that MongoDB is installed andrun successfully. Next time when you run MongoDB, you need to issue only commands.Install MongoDB on UbuntuRun the following command to import the MongoDB public GPG k ey −Create a /etc/apt/sources.list.d/mongodb.list file using the following command.Now issue the following command to update the repository −Next install the MongoDB by using the following command −In the above installation, 2.2.3 is currently released MongoDB version. Make sure to install the latest version always. Now MongoDB is installed successfully.Start MongoDBStop MongoDBRestart MongoDBTo use MongoDB run the following command.This will connect you to running MongoDB instance.MongoDB HelpTo get a list of commands, type db.help() in MongoDB client. This will give you a list of commands as shown in the following screenshot.1112MongoDB StatisticsTo get stats about MongoDB server, type the command db.stats() in MongoDB client. This will show the database name, number of collection and documents in the database. Output of the command is shown in the following screenshot.13Data in MongoDB has a flexible schema.documents in the same collection. They do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.Some considerations while designing Schema in MongoDB∙Design your schema according to user requirements.∙Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins).∙Duplicate the data (but limited) because disk space is cheap as compare to compute time.∙Do joins while write, not on read.∙Optimize your schema for most frequent use cases.∙Do complex aggregation in the schema.ExampleSuppose a client needs a database design for his blog/website and see the differences between RDBMS and MongoDB schema design. Website has the following requirements.∙Every post has the unique title, description and url.∙Every post can have one or more tags.∙Every post has the name of its publisher and total number of likes.∙Every post has comments given by users along with their name, message, data-time and likes.∙On each post, there can be zero or more comments.In RDBMS schema, design for above requirements will have minimum three tables.14While in MongoDB schema, design will have one collection post and the following structure:15So while showing the data, in RDBMS you need to join three tables and in MongoDB, data will be shown from one collection only.1617In this chapter, we will see how to create a database in MongoDB.The use CommandMongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database.SyntaxBasic syntax of use DATABASE statement is as follows: ExampleIf you want to create a database with name <mydb>, then use DATABASE statement would be as follows: To check your currently selected database, use the command db If you want to check your databases list, use the command show dbs . Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.MongoDB18In MongoDB default database is test. If you didn't create any database, then collections will be stored in test database.MongoDB19In this chapter, we will see how to drop a database using MongoDB command.The dropDatabase() MethodMongoDB db.dropDatabase() command is used to drop a existing database.SyntaxBasic syntax of dropDatabase() command is as follows: This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.ExampleFirst, check the list of available databases by using the command, show dbs . If you want to delete new database <mydb>, then dropDatabase() command would be as follows: Now check list of databases.In this chapter, we will see how to create a collection using MongoDB.The createCollection() MethodMongoDB db.createCollection(name, options) is used to create collection.SyntaxBasic syntax of createCollection() command is as follows:In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.Options parameter is optional, so you need to specify only the name of the collection. Following is the list of options you can use:2021While inserting the document, MongoDB first checks size field of capped collection, then it checks max field. ExamplesBasic syntax of createCollection() method without options is as follows:You can check the created collection by using the command show collections .The following example shows the syntax of createCollection() method with few important options:In MongoDB, you don't need to create collection. MongoDB creates collection automatically,when you insert some document.End of ebook previewIf you liked what you saw…Buy it from our store @ https://22。