成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

MongoDB學(xué)習(xí)筆記(1)- MongoDB簡介、數(shù)據(jù)類型及幫助命令

nihao / 2819人閱讀

摘要:數(shù)據(jù)模型取決于數(shù)據(jù)庫類型。僅支持位浮點(diǎn)數(shù),所以位整數(shù)會被自動轉(zhuǎn)換為位浮點(diǎn)數(shù)。位浮點(diǎn)數(shù)中的數(shù)字都是這種類型。數(shù)字只能表示為雙精度數(shù)位浮點(diǎn)數(shù)的另外一個問題是,有些位的整數(shù)并不能精確地表示為位浮點(diǎn)數(shù)。

MongoDB學(xué)習(xí)筆記(1)- MongoDB簡介及數(shù)據(jù)類型
本文所使用的MongoDB版本為 4.0.10
> db.version();
4.0.10
一、MongoDB 介紹 1. MongoDB 的特點(diǎn)

MongoDB 是一個可擴(kuò)展、高性能的 NoSQL 數(shù)據(jù)庫,由 C++ 語言編寫,旨在為 web 應(yīng)用提供高性能可擴(kuò)展的數(shù)據(jù)存儲解決方案。
它的特點(diǎn)是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便,主要特性有:

模式自由,支持動態(tài)查詢、完全索引,可輕易查詢文檔中內(nèi)嵌的對象及數(shù)組。

面向集合存儲,易存儲對象類型的數(shù)據(jù) , 包括文檔內(nèi)嵌對象及數(shù)組。

高效的數(shù)據(jù)存儲 , 支持二進(jìn)制數(shù)據(jù)及大型對象 ( 如照片和視頻 )。

支持復(fù)制和故障恢復(fù);提供了 主-從、主-主模式的數(shù)據(jù)復(fù)制及服務(wù)器之間的數(shù)據(jù)復(fù)制。

自動分片以支持云級別的伸縮性,支持水平的數(shù)據(jù)庫集群,可動態(tài)添加額外的服務(wù)器。

2. MongoDB的優(yōu)點(diǎn)與適用場景

MongoDB的優(yōu)點(diǎn)

高性能,速度非??欤ㄈ绻愕膬?nèi)存足夠的話)。

沒有固定的表結(jié)構(gòu),不用為了修改表結(jié)構(gòu)而進(jìn)行數(shù)據(jù)遷移。

查詢語言簡單,容易上手。

使用Sharding實(shí)現(xiàn)水平擴(kuò)展。

部署方便。

MongoDB的適用場景

適合作為信息基礎(chǔ)設(shè)施的持久化緩存層 。

適合實(shí)時的插入,更新與查詢,并具備應(yīng)用程序?qū)崟r數(shù)據(jù)存儲所需的復(fù)制及高度伸縮性。

Mongo 的 BSON 數(shù)據(jù)格式非常適合文檔化格式的存儲及查詢。

適合由數(shù)十或數(shù)百臺服務(wù)器組成的數(shù)據(jù)庫。因?yàn)镸ongo 已經(jīng)包含了對 MapReduce 引擎的內(nèi)置支持。

二、SQL 與 NoSQL對比
- SQL數(shù)據(jù)庫 NoSQL數(shù)據(jù)庫
類型 所有類型都支持SQL標(biāo)準(zhǔn) 存在多種類型,比如文檔存儲、鍵值存儲、列數(shù)據(jù)庫等
示例 MySQL、SQL Server、Oracle MongoDB、HBase、Cassandra
數(shù)據(jù)存儲模型 數(shù)據(jù)被存儲在表的行和列中,其中每一列都有一個特定類型。
表通常都是按照標(biāo)準(zhǔn)化原則創(chuàng)建的。
使用聯(lián)接來從多個表中檢索數(shù)據(jù)。
數(shù)據(jù)模型取決于數(shù)據(jù)庫類型。
比如數(shù)據(jù)被存儲為鍵值對以用于鍵值存儲。在基于文檔的數(shù)據(jù)庫中,數(shù)據(jù)會被存儲為文檔。
NoSQL的數(shù)據(jù)模型是靈活的,與SQL數(shù)據(jù)庫死板的表模型相反。
模式 固定的結(jié)構(gòu)和模式,因此對模式的任何變更都涉及修改數(shù)據(jù)庫 動態(tài)模式,通過擴(kuò)展或修改當(dāng)前模式就能適應(yīng)新的數(shù)據(jù)類型或結(jié)構(gòu)。
可以動態(tài)添加新的字段
可擴(kuò)展性 使用了縱向擴(kuò)展方式。這意味著隨著負(fù)荷的增加,需要購買更大、更貴的服務(wù)器來容納數(shù)據(jù)。 使用了橫向擴(kuò)展方式。這意味著可以將數(shù)據(jù)負(fù)荷分散到多臺廉價服務(wù)器上。
支持事務(wù) 支持ACID和事務(wù) 支持分區(qū)和可用性,會損害事務(wù)。
事務(wù)存在于某個級別,比如數(shù)據(jù)庫級別或文檔級別。
一致性 強(qiáng)一致性 取決于產(chǎn)品。有些產(chǎn)品選擇提供強(qiáng)一致性,而有些提供最終一致性。
查詢功能 可通過易用的GUI界面來使用 查詢可能需要編程專業(yè)技術(shù)和知識。與UI不同,其重心在于功能和編程接口
MongoDB 與 Mysql 概念對應(yīng)關(guān)系
mongodb mysql
數(shù)據(jù)庫(datebase) 數(shù)據(jù)庫(datebase)
集合(collection) 表(table)
文檔(document) 記錄(row)
字段 列 / 字段
索引 索引
嵌入和引用 表內(nèi)聯(lián)結(jié)
三、MongoDB 支持的數(shù)據(jù)類型 1. null

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

{ "x" : null }
2. 布爾

布爾類型有兩個值 true 和 false

{ "x": true }
3. 32位整數(shù)

在 Mongo Shell 中不支持這個類型。JavaScript僅支持64位浮點(diǎn)數(shù),所以32位整數(shù)會被自動轉(zhuǎn)換為64位浮點(diǎn)數(shù)。

4. 64位整數(shù)

在 Mongo Shell 中也不支持這個類型。Mongo Shell 會使用一個特殊的內(nèi)嵌文檔來顯示64位整數(shù)。

5. 64位浮點(diǎn)數(shù)

Mongo Shell 中的數(shù)字都是這種類型。

{  "pi" : 3.14 }
JavaScript 中只有一種 “數(shù)字” 類型。因?yàn)?MongoDB 中有3種數(shù)字類型(32位整數(shù)、64位整數(shù)和64位浮點(diǎn)數(shù)), shell 必須繞過 JavaScript 的限制。默認(rèn)情況下,shell 中的數(shù)字都被 MongoDB 當(dāng)做是雙精度數(shù)。這意味著如果你從數(shù)據(jù)庫中獲得的是一個32位整數(shù),修改文檔后,將文檔存回?cái)?shù)據(jù)庫的時候,這個整數(shù)也被轉(zhuǎn)換成了浮點(diǎn)數(shù),即便保持這個整數(shù)原封不動也會這樣的。所以明智的做法是盡量不要在 shell 下覆蓋整個文檔。

數(shù)字只能表示為雙精度數(shù)(64位浮點(diǎn)數(shù))的另外一個問題是,有些64位的整數(shù)并不能精確地表示為64位浮點(diǎn)數(shù)。所以,如果存入了一個64位整數(shù),在shell中查看,它會顯示為一個內(nèi)嵌文檔。但是在數(shù)據(jù)庫中實(shí)際存儲的值是準(zhǔn)確的。

32位的整數(shù)都能用64位的浮點(diǎn)數(shù)精確表示,所以顯示起來沒什么特別的。

6. 字符串

UTF-8字符串都可表示為字符串類型

{ "x" : "abcde" }
7. 對象id

對象id使用12字節(jié)的存儲空間,每個字節(jié)兩位十六進(jìn)制數(shù)字,是一個24位的字符串。

{ "_id" : ObjectId() }
8. 日期

日期類型存儲的是亳秒級的時間戳,不存儲時區(qū)。

{ "d" : new Date() }
9. 正則表達(dá)式

文檔中可以包含正則表達(dá)式,采用JavaScript的正則表達(dá)式語法。

{ "x" : /^abc/i }
10. 代碼

文檔中可以包含JavaScript代碼

{ "x" : function(){/********/} }
11. 數(shù)組

值的集合或者列表可以表示成數(shù)組

{ "d" : [1,2,3,4,5] }
12. 內(nèi)嵌文檔

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

{ "x" : { "y" : "z" } }
13. undefined

文檔中也可以使用 undefined((未定義)類型(JavaScript中 null 和 undefined 是不同的類型)。

{ "a" : undefined }
14. 二進(jìn)制數(shù)據(jù)

二進(jìn)制數(shù)據(jù)可以由任意字節(jié)的串組成??捎糜诖鎯D片等二進(jìn)制文件。不過在 Mongo Shell 中無法使用。

四、Mongo Shell 幫助命令 1. 系統(tǒng)級幫助: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

        # 顯示所有數(shù)據(jù)庫
        show dbs                     show database names
        # 顯示所有集合
        show collections             show collections in current database
        # 顯示當(dāng)前數(shù)據(jù)庫所有用戶
        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                 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
2. 查看數(shù)據(jù)庫上可用的操作: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
        # 刪除數(shù)據(jù)庫
        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
        # 查看當(dāng)前數(shù)據(jù)庫中的所有集合
        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
        # 集合中的記錄數(shù)
        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 } )
        # 刪除集合內(nèi)的所有索引
        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: , indexDetailsName: })
        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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/106199.html

相關(guān)文章

  • MongoDB 學(xué)習(xí)筆記

    摘要:本文內(nèi)容主要來自的學(xué)習(xí),學(xué)習(xí)筆記基于個人理解對原書部分內(nèi)容進(jìn)行調(diào)整。如果需要練習(xí)相關(guān)命令行工具可直接閱讀本學(xué)習(xí)筆記。筆者測試數(shù)據(jù)庫版本較早,但文中涉及的所有概念及命令行工具基本適用于所有版本。二準(zhǔn)備安裝和運(yùn)行服務(wù)在學(xué)習(xí)之前,需要安裝環(huán)境。 感謝 Karl Seguin 編寫的 The Little MongoDB Book 這本 MongoDB 入門書。 本文內(nèi)容主要來自「The Li...

    makeFoxPlay 評論0 收藏0
  • MongoDB 學(xué)習(xí)筆記

    摘要:本文內(nèi)容主要來自的學(xué)習(xí),學(xué)習(xí)筆記基于個人理解對原書部分內(nèi)容進(jìn)行調(diào)整。如果需要練習(xí)相關(guān)命令行工具可直接閱讀本學(xué)習(xí)筆記。筆者測試數(shù)據(jù)庫版本較早,但文中涉及的所有概念及命令行工具基本適用于所有版本。二準(zhǔn)備安裝和運(yùn)行服務(wù)在學(xué)習(xí)之前,需要安裝環(huán)境。 感謝 Karl Seguin 編寫的 The Little MongoDB Book 這本 MongoDB 入門書。 本文內(nèi)容主要來自「The Li...

    劉永祥 評論0 收藏0
  • mongoDB 學(xué)習(xí)筆記純干貨(mongoose、增刪改查、聚合、索引、連接、備份與恢復(fù)、監(jiān)控等等)

    摘要:集合名命名規(guī)范集合名不能是空字符串。集合名不能含有字符空字符,這個字符表示集合名的結(jié)尾。集合名不能以開頭,這是為系統(tǒng)集合保留的前綴。有些驅(qū)動程序的確支持在集合名里面包含,這是因?yàn)槟承┫到y(tǒng)生成的集合中包含該字符。 原始文章鏈接 - 我的博客:http://www.lovebxm.com/2017/0... MongoDB - 簡介 官網(wǎng):https://www.mongodb.com/ ...

    Java3y 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<