本文所使用的MongoDB版本为 4.0.10
> db.version(); 4.0.10
MongoDB 是一个可扩展、高性能的 NoSQL 数据库,由 C++ 语言编写,旨在为 web 应用提供高性能可扩展的数据存储解决方案。
它的特色是高性能、易部署、易使用,存储数据很是方便,主要特性有:mysql
MongoDB的优势web
MongoDB的适用场景正则表达式
- | SQL数据库 | NoSQL数据库 |
---|---|---|
类型 | 全部类型都支持SQL标准 | 存在多种类型,好比文档存储、键值存储、列数据库等 |
示例 | MySQL、SQL Server、Oracle | MongoDB、HBase、Cassandra |
数据存储模型 | 数据被存储在表的行和列中,其中每一列都有一个特定类型。 表一般都是按照标准化原则建立的。 使用联接来从多个表中检索数据。 |
数据模型取决于数据库类型。 好比数据被存储为键值对以用于键值存储。在基于文档的数据库中,数据会被存储为文档。 NoSQL的数据模型是灵活的,与SQL数据库死板的表模型相反。 |
模式 | 固定的结构和模式,所以对模式的任何变动都涉及修改数据库 | 动态模式,经过扩展或修改当前模式就能适应新的数据类型或结构。 能够动态添加新的字段 |
可扩展性 | 使用了纵向扩展方式。这意味着随着负荷的增长,须要购买更大、更贵的服务器来容纳数据。 | 使用了横向扩展方式。这意味着能够将数据负荷分散到多台廉价服务器上。 |
支持事务 | 支持ACID和事务 | 支持分区和可用性,会损害事务。 事务存在于某个级别,好比数据库级别或文档级别。 |
一致性 | 强一致性 | 取决于产品。有些产品选择提供强一致性,而有些提供最终一致性。 |
查询功能 | 可经过易用的GUI界面来使用 | 查询可能须要编程专业技术和知识。与UI不一样,其重心在于功能和编程接口 |
mongodb | mysql |
---|---|
数据库(datebase) | 数据库(datebase) |
集合(collection) | 表(table) |
文档(document) | 记录(row) |
字段 | 列 / 字段 |
索引 | 索引 |
嵌入和引用 | 表内联结 |
null用于表示空值或不存在的字段sql
{ "x" : null }
布尔类型有两个值 true 和 falsemongodb
{ "x": true }
在 Mongo Shell 中不支持这个类型。JavaScript仅支持64位浮点数,因此32位整数会被自动转换为64位浮点数。shell
在 Mongo Shell 中也不支持这个类型。Mongo Shell 会使用一个特殊的内嵌文档来显示64位整数。数据库
Mongo Shell 中的数字都是这种类型。编程
{ "pi" : 3.14 }
JavaScript 中只有一种 “数字” 类型。由于 MongoDB 中有3种数字类型(32位整数、64位整数和64位浮点数), shell 必须绕过 JavaScript 的限制。默认状况下,shell 中的数字都被 MongoDB 当作是双精度数。这意味着若是你从数据库中得到的是一个32位整数,修改文档后,将文档存回数据库的时候,这个整数也被转换成了浮点数,即使保持这个整数原封不动也会这样的。因此明智的作法是尽可能不要在 shell 下覆盖整个文档。数字只能表示为双精度数(64位浮点数)的另一个问题是,有些64位的整数并不能精确地表示为64位浮点数。因此,若是存入了一个64位整数,在shell中查看,它会显示为一个内嵌文档。可是在数据库中实际存储的值是准确的。json
32位的整数都能用64位的浮点数精确表示,因此显示起来没什么特别的。数组
UTF-8字符串均可表示为字符串类型
{ "x" : "abcde" }
对象id使用12字节的存储空间,每一个字节两位十六进制数字,是一个24位的字符串。
{ "_id" : ObjectId() }
日期类型存储的是亳秒级的时间戳,不存储时区。
{ "d" : new Date() }
文档中能够包含正则表达式,采用JavaScript的正则表达式语法。
{ "x" : /^abc/i }
文档中能够包含JavaScript代码
{ "x" : function(){/********/} }
值的集合或者列表能够表示成数组
{ "d" : [1,2,3,4,5] }
文档中能够包含其余文档,也能够做为值嵌入到父文档中。
{ "x" : { "y" : "z" } }
文档中也可使用 undefined((未定义)类型(JavaScript中 null 和 undefined 是不一样的类型)。
{ "a" : undefined }
二进制数据能够由任意字节的串组成。可用于存储图片等二进制文件。不过在 Mongo Shell 中没法使用。
> 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 database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> 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 shell exit quit the mongo shell
> 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,<component>) db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db db.unsetWriteConcern(<write concern doc>) - 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
> db.user.help() DBCollection help db.user.find().help() - show DBCursor help db.user.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j # 集合中的记录数 db.user.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS db.user.countDocuments( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS db.user.estimatedDocumentCount( <optional params> ) - 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], <options>) # 集合大小 db.user.dataSize() db.user.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j db.user.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j db.user.distinct( key, query, <optional params> ) - 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, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS db.user.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument db.user.findOneAndUpdate( filter, update, <optional params> ) - 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, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j db.user.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j db.user.mapReduce( mapFunction , reduceFunction , <optional params> ) db.user.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor db.user.remove(query) db.user.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j db.user.renameCollection( newName , <dropTarget> ) renames the collection. db.user.runCommand( name , <options> ) 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, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j db.user.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j db.user.validate( <full> ) - SLOW db.user.getShardVersion() - only for use with sharding db.user.getShardDistribution() - prints statistics about data distribution in the cluster db.user.getSplitKeysForChunks( <maxChunkSize> ) - 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( <write concern doc> ) - sets the write concern for writes to the collection db.user.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection db.user.latencyStats() - display operation latency histograms for this collection