欢迎点击上方蓝字关注我

本文所使用的MongoDB版本为 4.0.10

> db.version();4.0.10

一、MongoDB 介绍

1. MongoDB 的特点

MongoDB 是一个可扩展、高性能的 NoSQL 数据库,由 C++ 语言编写,旨在为 web 应用提供高性能可扩展的数据存储解决方案。
它的特点是高性能、易部署、易使用,存储数据非常方便,主要特性有:

  • 模式自由,支持动态查询、完全索引,可轻易查询文档中内嵌的对象及数组。

  • 面向集合存储,易存储对象类型的数据 , 包括文档内嵌对象及数组。

  • 高效的数据存储 , 支持二进制数据及大型对象 ( 如照片和视频 )。

  • 支持复制和故障恢复;提供了 主-从、主-主模式的数据复制及服务器之间的数据复制。

  • 自动分片以支持云级别的伸缩性,支持水平的数据库集群,可动态添加额外的服务器。

2. MongoDB的优点与适用场景

MongoDB的优点

  • 高性能,速度非常快(如果你的内存足够的话)。

  • 没有固定的表结构,不用为了修改表结构而进行数据迁移。

  • 查询语言简单,容易上手。

  • 使用Sharding实现水平扩展。

  • 部署方便。

MongoDB的适用场景

  • 适合作为信息基础设施的持久化缓存层 。

  • 适合实时的插入,更新与查询,并具备应用程序实时数据存储所需的复制及高度伸缩性。

  • Mongo 的 BSON 数据格式非常适合文档化格式的存储及查询。

  • 适合由数十或数百台服务器组成的数据库。因为Mongo 已经包含了对 MapReduce 引擎的内置支持。


二、SQL 与 NoSQL对比

MongoDB 与 Mysql 概念对应关系

mongodb mysql
数据库(datebase) 数据库(datebase)
集合(collection) 表(table)
文档(document) 记录(row)
字段 列 / 字段
索引 索引
嵌入和引用 表内联结

三、MongoDB 支持的数据类型

1. null

null用于表示空值或不存在的字段

{ "x" : null }

2. 布尔

布尔类型有两个值 true 和 false

{ "x": true }

3. 32位整数

在 Mongo Shell 中不支持这个类型。JavaScript仅支持64位浮点数,所以32位整数会被自动转换为64位浮点数。

4. 64位整数

在 Mongo Shell 中也不支持这个类型。Mongo Shell 会使用一个特殊的内嵌文档来显示64位整数。

5. 64位浮点数

Mongo Shell 中的数字都是这种类型。

{ "pi" : 3.14 }

JavaScript 中只有一种 “数字” 类型。因为 MongoDB 中有3种数字类型(32位整数、64位整数和64位浮点数), shell 必须绕过 JavaScript 的限制。默认情况下,shell 中的数字都被 MongoDB 当做是双精度数。这意味着如果你从数据库中获得的是一个32位整数,修改文档后,将文档存回数据库的时候,这个整数也被转换成了浮点数,即便保持这个整数原封不动也会这样的。所以明智的做法是尽量不要在 shell 下覆盖整个文档。

数字只能表示为双精度数(64位浮点数)的另外一个问题是,有些64位的整数并不能精确地表示为64位浮点数。所以,如果存入了一个64位整数,在shell中查看,它会显示为一个内嵌文档。但是在数据库中实际存储的值是准确的。

32位的整数都能用64位的浮点数精确表示,所以显示起来没什么特别的。

6. 字符串

UTF-8字符串都可表示为字符串类型

{ "x" : "abcde" }

7. 对象id

对象id使用12字节的存储空间,每个字节两位十六进制数字,是一个24位的字符串。

{ "_id" : ObjectId() }

8. 日期

日期类型存储的是亳秒级的时间戳,不存储时区。

{ "d" : new Date() }

9. 正则表达式

文档中可以包含正则表达式,采用JavaScript的正则表达式语法。

{ "x" : /^abc/i }

10. 代码

文档中可以包含JavaScript代码

{ "x" : function(){/********/} }

11. 数组

值的集合或者列表可以表示成数组

{ "d" : [1,2,3,4,5] }

12. 内嵌文档

文档中可以包含其他文档,也可以作为值嵌入到父文档中。

{ "x" : { "y" : "z" } }

13. undefined

文档中也可以使用 undefined((未定义)类型(JavaScript中 null 和 undefined 是不同的类型)。

{ "a" : undefined }

14. 二进制数据

二进制数据可以由任意字节的串组成。可用于存储图片等二进制文件。不过在 Mongo Shell 中无法使用。


四、Mongo Shell 帮助命令

1. 系统级帮助:help

> help        db.help()                    help on db methods        db.mycoll.help()             help on collection methods        sh.help()                    sharding helpers        rs.help()                    replica set helpers        help admin                   administrative help        help connect                 connecting to a db help        help keys                    key shortcuts        help misc                    misc things to know        help mr                      mapreduce

        # 显示所有数据库show dbs                     show database names        # 显示所有集合show collections             show collections in current database        # 显示当前数据库所有用户show users                   show users in current databaseshow profile                 show most recent system.profile entries with time >= 1msshow logs                    show the accessible logger namesshow log [name]              prints out the last segment of log in memory, 'global' is default        use set current database        db.foo.find()                list objects in collection foo        db.foo.find( { a : 1 } )     list objects in foo where a == 1        it                           result of the last line evaluated; use to further iterate        DBQuery.shellBatchSize = x   set default number of items to display on shellexit                         quit the mongo shell

2. 查看数据库上可用的操作:db.help()

> db.help()DB methods:        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor        db.auth(username, password)        db.cloneDatabase(fromhost) - deprecated        db.commandHelp(name) returns the help for the command        db.copyDatabase(fromdb, todb, fromhost) - deprecated        db.createCollection(name, {size: ..., capped: ..., max: ...})        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})        db.createUser(userDocument)        db.currentOp() displays currently executing operations in the db        # 删除数据库        db.dropDatabase()        db.eval() - deprecated        db.fsyncLock() flush data to disk and lock server for backups        db.fsyncUnlock() unlocks server following a db.fsyncLock()        db.getCollection(cname) same as db['cname'] or db.cname        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections        # 查看当前数据库中的所有集合        db.getCollectionNames()        db.getLastError() - just returns the err msg string        db.getLastErrorObj() - return full status object        db.getLogComponents()        db.getMongo() get the server connection object        db.getMongo().setSlaveOk() allow queries on a replication slave server        db.getName()        db.getPrevError()        db.getProfilingLevel() - deprecated        db.getProfilingStatus() - returns if profiling is on and slow threshold        db.getReplicationInfo()        db.getSiblingDB(name) get the db at the same server as this one        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set        db.hostInfo() get details about the server's host        db.isMaster() check replica primary status        db.killOp(opid) kills the current operation in the db        db.listCommands() lists all the db commands        db.loadServerScripts() loads all the scripts in db.system.js        db.logout()        db.printCollectionStats()        db.printReplicationInfo()        db.printShardingStatus()        db.printSlaveReplicationInfo()        db.dropUser(username)        db.repairDatabase()        db.resetError()        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into {cmdObj: 1}        db.serverStatus()        db.setLogLevel(level,)        db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all        db.setWriteConcern() - sets the write concern for writes to the db        db.unsetWriteConcern() - unsets the write concern for writes to the db        db.setVerboseShell(flag) display extra information in shell output        db.shutdownServer()        db.stats()        db.version() current version of the server

3. 查看集合上可用的操作:db.集合名.help()

> db.user.help()DBCollection help        db.user.find().help() - show DBCursor help        db.user.bulkWrite( operations,  ) - bulk execute write operations, optional parameters are: w, wtimeout, j        # 集合中的记录数        db.user.count( query = {},  ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS        db.user.countDocuments( query = {},  ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS        db.user.estimatedDocumentCount(  ) - estimate the document count using collection metadata, optional parameters are: maxTimeMS        db.user.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.        db.user.convertToCapped(maxBytes) - calls {convertToCapped:'user', size:maxBytes}} command        db.user.createIndex(keypattern[,options])        db.user.createIndexes([keypatterns], )        # 集合大小        db.user.dataSize()        db.user.deleteOne( filter,  ) - delete first matching document, optional parameters are: w, wtimeout, j        db.user.deleteMany( filter,  ) - delete all matching documents, optional parameters are: w, wtimeout, j        db.user.distinct( key, query,  ) - e.g. db.user.distinct( 'x' ), optional parameters are: maxTimeMS        # 删除集合        db.user.drop() drop the collection        db.user.dropIndex(index) - e.g. db.user.dropIndex( "indexName" ) or db.user.dropIndex( { "indexKey" : 1 } )        # 删除集合内的所有索引        db.user.dropIndexes()        db.user.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead        db.user.explain().help() - show explain help        db.user.reIndex()        db.user.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.e.g. db.user.find( {x:77} , {name:1, x:1} )        db.user.find(...).count()        db.user.find(...).limit(n)        db.user.find(...).skip(n)        db.user.find(...).sort(...)        db.user.findOne([query], [fields], [options], [readConcern])        db.user.findOneAndDelete( filter,  ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS        db.user.findOneAndReplace( filter, replacement,  ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument        db.user.findOneAndUpdate( filter, update,  ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument        db.user.getDB() get DB object associated with collection        db.user.getPlanCache() get query plan cache associated with collection        db.user.getIndexes()        db.user.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )        db.user.insert(obj)        db.user.insertOne( obj,  ) - insert a document, optional parameters are: w, wtimeout, j        db.user.insertMany( [objects],  ) - insert multiple documents, optional parameters are: w, wtimeout, j        db.user.mapReduce( mapFunction , reduceFunction ,  )        db.user.aggregate( [pipeline],  ) - performs an aggregation on a collection; returns a cursor        db.user.remove(query)        db.user.replaceOne( filter, replacement,  ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j        db.user.renameCollection( newName ,  ) renames the collection.        db.user.runCommand( name ,  ) runs a db command with the given name where the first param is the collection name        db.user.save(obj)        db.user.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})        db.user.storageSize() - includes free space allocated to this collection        db.user.totalIndexSize() - size in bytes of all the indexes        db.user.totalSize() - storage allocated for all data and indexes        db.user.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi        db.user.updateOne( filter, update,  ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j        db.user.updateMany( filter, update,  ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j        db.user.validate(  ) - SLOW        db.user.getShardVersion() - only for use with sharding        db.user.getShardDistribution() - prints statistics about data distribution in the cluster        db.user.getSplitKeysForChunks(  ) - calculates split points over all chunks and returns splitter function        db.user.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set        db.user.setWriteConcern(  ) - sets the write concern for writes to the collection        db.user.unsetWriteConcern(  ) - unsets the write concern for writes to the collection        db.user.latencyStats() - display operation latency histograms for this collection

点个在看升职加薪

mongodb 数字 _id_MongoDB学习笔记MongoDB简介及数据类型相关推荐

  1. 转载:mongoDB java驱动学习笔记

    http://www.blogjava.net/watchzerg/archive/2012/09/22/388346.html mongoDB java驱动学习笔记 指定新mongo实例: Mong ...

  2. 数字图像处理学习笔记(三):ORB算法(尺度不变特征变换)Oriented FAST and Rotated BRIEF

    数字图像处理学习笔记(三):ORB算法(尺度不变特征变换)Oriented FAST and Rotated BRIEF 一.概述 参考:特征点匹配+特征检测方法汇总 ORB的全称是Oriented ...

  3. 数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法

    数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法 一.概述: 提到特征点算法,首先就是大名鼎鼎的SIFT算法了.SIFT的全称是Scale Invariant Feature Transf ...

  4. 数字图像处理学习笔记(一):特征检测和匹配概述

    数字图像处理学习笔记(一):特征检测和匹配概述 参考博客: 特征点的匹配 SIFT特征详解 数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法 1.特征点概述 如何高效且准确的匹配出两个不同 ...

  5. 数字图像处理学习笔记(三)——空间分辨率和灰度分辨率、等偏爱曲线

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  6. 基于python的数字图像处理--学习笔记(三)

    基于python的数字图像处理--学习笔记(三) 前言 一.灰度拉伸 二.幂律(伽马)变换 三.对数变换 前言 进入冈萨雷斯的第三章内容,并用python实现功能.我更改了代码源,之前找到太烂了,代码 ...

  7. 数字图像处理学习笔记(十五)——图像复原与重建

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  8. 数字图像处理学习笔记(六)——数字图像处理中用到的数学操作

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  9. 数字图像处理学习笔记(八)——图像增强处理方法之点处理

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

最新文章

  1. golang垃圾回收概述
  2. 选择Java接口还是抽象类
  3. Ubuntu安装Ceres库-安装依赖时报错:E:无法定位软件包 libcxsparse3.1.2_朱国鑫的博客-CSDN博客
  4. http与https的区别,http 1.0与1.1的区别
  5. 九、表达式求值(1)
  6. linux arp 老化时间,Linux实现的ARP缓存老化时间原理解析
  7. 【渝粤教育】国家开放大学2018年春季 0689-22T老年心理健康 参考试题
  8. 内存泄漏Valgrind
  9. C++学习之路 | PTA乙级—— 1076 Wifi密码 (15 分)(精简)
  10. java xml opencv_Java中使用opencv
  11. OJ1024: 计算字母序号
  12. 基于Yarn的Spark环境,统计哈姆雷特词频(1)
  13. js中的children实时获取子元素
  14. 【2022 李宏毅】机器学习导论
  15. 水处理过滤器运行特性及选择原则浅谈
  16. Docker网络及资源管理
  17. android闹钟报告分析,Android AlarmClock 闹钟应用 简单分析
  18. 戴尔游匣5577黑苹果EFI文件
  19. 策略迭代与值迭代的区别
  20. 丁香园php岗_你为什么从丁香园离职?

热门文章

  1. SAP Cloud for Customer里的HTML Mashup的JavaScript编程功能
  2. 推荐一个在线图片处理神奇,图片处理绝大多数需求,都能在浏览器里搞定
  3. SAP CRM Product category的决定逻辑
  4. 利用S_MEMORY_INSPECTOR分析内存泄漏问题
  5. OPA 17 - searchField.setValue
  6. CRM webClient UI搜索参数里max hit是怎么被后台服务器处理的
  7. COMMIT WORK关键字在CRM content management应用里的使用场景
  8. document builder how is document url being generated
  9. PPR data model
  10. 替换SAP Fiori Logo不成功的workaround