更新 '数据库/mongodb/MongoDB数据表基本操作.md'
This commit is contained in:
parent
69add563c1
commit
9805c4a102
2 changed files with 78 additions and 210 deletions
|
@ -1,210 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="tool" content="leanote-desktop-app">
|
|
||||||
<title>MongoDB数据表基本操作</title>
|
|
||||||
<style>
|
|
||||||
|
|
||||||
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*公用文字样式*/
|
|
||||||
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
|
|
||||||
.note-container{
|
|
||||||
width:850px;
|
|
||||||
margin:auto;
|
|
||||||
padding: 10px 20px;
|
|
||||||
box-shadow: 1px 1px 10px #eee;
|
|
||||||
}
|
|
||||||
#title {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
table {
|
|
||||||
margin-bottom: 16px;
|
|
||||||
border-collapse: collapse;
|
|
||||||
}
|
|
||||||
table th, table td {
|
|
||||||
padding: 6px 13px;
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
}
|
|
||||||
table th {
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
|
|
||||||
table tr {
|
|
||||||
background-color: none;
|
|
||||||
border-top: 1px solid #ccc;
|
|
||||||
}
|
|
||||||
table tr:nth-child(2n) {
|
|
||||||
background-color: rgb(247, 247, 249);
|
|
||||||
}
|
|
||||||
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
border-collapse: collapse;
|
|
||||||
padding: 6px 13px;
|
|
||||||
}
|
|
||||||
blockquote {
|
|
||||||
border-left-width:10px;
|
|
||||||
background-color:rgba(128,128,128,0.05);
|
|
||||||
border-top-right-radius:5px;
|
|
||||||
border-bottom-right-radius:5px;
|
|
||||||
padding:15px 20px;
|
|
||||||
border-left:5px solid rgba(128,128,128,0.075);
|
|
||||||
}
|
|
||||||
blockquote p {
|
|
||||||
margin-bottom:1.1em;
|
|
||||||
font-size:1em;
|
|
||||||
line-height:1.45
|
|
||||||
}
|
|
||||||
blockquote ul:last-child,blockquote ol:last-child {
|
|
||||||
margin-bottom:0
|
|
||||||
}
|
|
||||||
pre {
|
|
||||||
padding: 18px;
|
|
||||||
background-color: #f7f7f9;
|
|
||||||
border: 1px solid #e1e1e8;
|
|
||||||
border-radius: 3px;
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
code {
|
|
||||||
padding: 2px 4px;
|
|
||||||
font-size: 90%;
|
|
||||||
color: #c7254e;
|
|
||||||
white-space: nowrap;
|
|
||||||
background-color: #f9f2f4;
|
|
||||||
border-radius: 4px;
|
|
||||||
}
|
|
||||||
.footnote {
|
|
||||||
vertical-align: top;
|
|
||||||
position: relative;
|
|
||||||
top: -0.5em;
|
|
||||||
font-size: .8em;
|
|
||||||
}
|
|
||||||
|
|
||||||
hr {
|
|
||||||
margin:2em 0
|
|
||||||
}
|
|
||||||
img {
|
|
||||||
max-width:100%
|
|
||||||
}
|
|
||||||
pre {
|
|
||||||
word-break:break-word
|
|
||||||
}
|
|
||||||
p,pre,pre.prettyprint,blockquote {
|
|
||||||
margin:0 0 1.1em
|
|
||||||
}
|
|
||||||
hr {
|
|
||||||
margin:2em 0
|
|
||||||
}
|
|
||||||
img {
|
|
||||||
max-width:100%
|
|
||||||
}
|
|
||||||
.sequence-diagram,.flow-chart {
|
|
||||||
text-align:center;
|
|
||||||
margin-bottom:1.1em
|
|
||||||
}
|
|
||||||
.sequence-diagram text,.flow-chart text {
|
|
||||||
font-size:15px !important;
|
|
||||||
font-family:"Source Sans Pro",sans-serif !important
|
|
||||||
}
|
|
||||||
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
|
|
||||||
fill:#f6f6f6
|
|
||||||
}
|
|
||||||
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
|
|
||||||
stroke:#3f3f3f
|
|
||||||
}
|
|
||||||
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
|
|
||||||
stroke:none
|
|
||||||
}
|
|
||||||
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
|
|
||||||
fill:#3f3f3f
|
|
||||||
}
|
|
||||||
ul,ol {
|
|
||||||
margin-bottom:1.1em
|
|
||||||
}
|
|
||||||
ul ul,ol ul,ul ol,ol ol {
|
|
||||||
margin-bottom:1.1em
|
|
||||||
}
|
|
||||||
kbd {
|
|
||||||
padding:.1em .6em;
|
|
||||||
border:1px solid rgba(63,63,63,0.25);
|
|
||||||
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
|
||||||
box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
|
||||||
font-size:.7em;
|
|
||||||
font-family:sans-serif;
|
|
||||||
background-color:#fff;
|
|
||||||
color:#333;
|
|
||||||
border-radius:3px;
|
|
||||||
display:inline-block;
|
|
||||||
margin:0 .1em;
|
|
||||||
white-space:nowrap
|
|
||||||
}
|
|
||||||
.toc ul {
|
|
||||||
list-style-type:none;
|
|
||||||
margin-bottom:15px
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<!-- 该css供自定义样式 -->
|
|
||||||
<link href="../leanote-html.css" rel="stylesheet">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="note-container">
|
|
||||||
<h1 class="title" id="leanote-title">MongoDB数据表基本操作</h1>
|
|
||||||
<div class="content-html" id="leanote-content"><p>查看全部数据表</p><pre id="leanote_ace_1479781232163_0" class="brush:sh ace-tomorrow">> use ChatRoom
|
|
||||||
switched to db ChatRoom
|
|
||||||
> show collections
|
|
||||||
Account
|
|
||||||
Chat
|
|
||||||
system.indexes
|
|
||||||
system.users</pre><p>创建数据表</p><pre id="leanote_ace_1479781278873_0" class="brush:sh ace-tomorrow">> db.createCollection("Account")
|
|
||||||
{"ok":1}</pre><pre id="leanote_ace_1479781293825_0" class="brush:sh ace-tomorrow">> db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 }
|
|
||||||
{"ok":1}</pre><p>-- 说明</p><p>capped:true,表示该集合的结构不能被修改;</p><p>size:在建表之初就指定一定的空间大小,接下来的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中,如果已经超出空间大小,则回到文件头覆盖原来的数据继续插入。这种结构保证了插入和查询的高效性,它不允许删除单个记录,更新的也有限制:不能超过原有记录的大小。这种表效率很高,它适用于一些暂时保存数据的场合,比如网站中登录用户的session信息,又比如一些程序的监控日志,都是属于过了一定的时间就可以被覆盖的数据。</p><p> <br></p><p>修改数据表名</p><pre id="leanote_ace_1479781320849_0" class="brush:sh ace-tomorrow">> db.Account.renameCollection("Account1")
|
|
||||||
{ "ok" : 1 }</pre><p> <br></p><p>数据表帮助主题help</p><pre id="leanote_ace_1479781866930_0" class="brush:sh ace-tomorrow">> db.Account.help()
|
|
||||||
DBCollection help
|
|
||||||
db.Account.find().help() - show DBCursor help
|
|
||||||
db.Account.count()
|
|
||||||
db.Account.dataSize()
|
|
||||||
db.Account.distinct( key ) - eg. db.Account.distinct( 'x' )
|
|
||||||
db.Account.drop() drop the collection
|
|
||||||
db.Account.dropIndex(name)
|
|
||||||
db.Account.dropIndexes()
|
|
||||||
db.Account.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
|
|
||||||
db.Account.reIndex()
|
|
||||||
db.Account.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to retur.
|
|
||||||
|
|
||||||
e.g. db.Account.find( {x:77} , {name:1, x:1} )
|
|
||||||
db.Account.find(...).count()
|
|
||||||
db.Account.find(...).limit(n)
|
|
||||||
db.Account.find(...).skip(n)
|
|
||||||
db.Account.find(...).sort(...)
|
|
||||||
db.Account.findOne([query])
|
|
||||||
db.Account.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
|
|
||||||
db.Account.getDB() get DB object associated with collection
|
|
||||||
db.Account.getIndexes()
|
|
||||||
db.Account.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
|
|
||||||
db.Account.mapReduce( mapFunction , reduceFunction , <optional params> )
|
|
||||||
db.Account.remove(query)
|
|
||||||
db.Account.renameCollection( newName , <dropTarget> ) renames the collection.
|
|
||||||
db.Account.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
|
|
||||||
db.Account.save(obj)
|
|
||||||
db.Account.stats()
|
|
||||||
db.Account.storageSize() - includes free space allocated to this collection
|
|
||||||
db.Account.totalIndexSize() - size in bytes of all the indexes
|
|
||||||
db.Account.totalSize() - storage allocated for all data and indexes
|
|
||||||
db.Account.update(query, object[, upsert_bool, multi_bool])
|
|
||||||
db.Account.validate() - SLOW
|
|
||||||
db.Account.getShardVersion() - only for use with sharding </pre><p> <br></p><p>查看全部表记录</p><pre id="leanote_ace_1479782012602_0" class="brush:sh ace-tomorrow">> db.Account.find()
|
|
||||||
{ "_id" : ObjectId("4df08553188e444d001a763a"), "AccountID" : 1, "UserName" : "libing", "Password" : "1", "Age" : 26, "Email" : "libing@126.com", "RegisterDate" : "2011-06-09 16:31:25" }
|
|
||||||
{ "_id" : ObjectId("4df08586188e444d001a763b"), "AccountID" : 2, "UserName" : "lb", "Password" : "1", "Age" : 25, "Email" : "libing@163.com", "RegisterDate" : "2011-06-09 16:36:95" }</pre><p> <br></p></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 该js供其它处理 -->
|
|
||||||
<script src="../leanote-html.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
78
数据库/mongodb/MongoDB数据表基本操作.md
Normal file
78
数据库/mongodb/MongoDB数据表基本操作.md
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
# MongoDB数据表基本操作
|
||||||
|
## 查看全部数据表
|
||||||
|
```bash
|
||||||
|
> use ChatRoom
|
||||||
|
switched to db ChatRoom
|
||||||
|
> show collections
|
||||||
|
Account
|
||||||
|
Chat
|
||||||
|
system.indexes
|
||||||
|
system.users
|
||||||
|
```
|
||||||
|
## 创建数据表
|
||||||
|
```bash
|
||||||
|
> db.createCollection("Account")
|
||||||
|
{"ok":1}
|
||||||
|
> db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 }
|
||||||
|
{"ok":1}
|
||||||
|
```
|
||||||
|
-- 说明
|
||||||
|
|
||||||
|
capped:true,表示该集合的结构不能被修改;
|
||||||
|
|
||||||
|
size:在建表之初就指定一定的空间大小,接下来的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中,如果已经超出空间大小,则回到文件头覆盖原来的数据继续插入。这种结构保证了插入和查询的高效性,它不允许删除单个记录,更新的也有限制:不能超过原有记录的大小。这种表效率很高,它适用于一些暂时保存数据的场合,比如网站中登录用户的session信息,又比如一些程序的监控日志,都是属于过了一定的时间就可以被覆盖的数据。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 修改数据表名
|
||||||
|
```bash
|
||||||
|
> db.Account.renameCollection("Account1")
|
||||||
|
{ "ok" : 1 }
|
||||||
|
```
|
||||||
|
|
||||||
|
## 数据表帮助主题help
|
||||||
|
```bash
|
||||||
|
> db.Account.help()
|
||||||
|
DBCollection help
|
||||||
|
db.Account.find().help() - show DBCursor help
|
||||||
|
db.Account.count()
|
||||||
|
db.Account.dataSize()
|
||||||
|
db.Account.distinct( key ) - eg. db.Account.distinct( 'x' )
|
||||||
|
db.Account.drop() drop the collection
|
||||||
|
db.Account.dropIndex(name)
|
||||||
|
db.Account.dropIndexes()
|
||||||
|
db.Account.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
|
||||||
|
db.Account.reIndex()
|
||||||
|
db.Account.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to retur.
|
||||||
|
|
||||||
|
e.g. db.Account.find( {x:77} , {name:1, x:1} )
|
||||||
|
db.Account.find(...).count()
|
||||||
|
db.Account.find(...).limit(n)
|
||||||
|
db.Account.find(...).skip(n)
|
||||||
|
db.Account.find(...).sort(...)
|
||||||
|
db.Account.findOne([query])
|
||||||
|
db.Account.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
|
||||||
|
db.Account.getDB() get DB object associated with collection
|
||||||
|
db.Account.getIndexes()
|
||||||
|
db.Account.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
|
||||||
|
db.Account.mapReduce( mapFunction , reduceFunction , <optional params> )
|
||||||
|
db.Account.remove(query)
|
||||||
|
db.Account.renameCollection( newName , <dropTarget> ) renames the collection.
|
||||||
|
db.Account.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
|
||||||
|
db.Account.save(obj)
|
||||||
|
db.Account.stats()
|
||||||
|
db.Account.storageSize() - includes free space allocated to this collection
|
||||||
|
db.Account.totalIndexSize() - size in bytes of all the indexes
|
||||||
|
db.Account.totalSize() - storage allocated for all data and indexes
|
||||||
|
db.Account.update(query, object[, upsert_bool, multi_bool])
|
||||||
|
db.Account.validate() - SLOW
|
||||||
|
db.Account.getShardVersion() - only for use with sharding
|
||||||
|
```
|
||||||
|
|
||||||
|
## 查看全部表记录
|
||||||
|
```bash
|
||||||
|
> db.Account.find()
|
||||||
|
{ "_id" : ObjectId("4df08553188e444d001a763a"), "AccountID" : 1, "UserName" : "libing", "Password" : "1", "Age" : 26, "Email" : "libing@126.com", "RegisterDate" : "2011-06-09 16:31:25" }
|
||||||
|
{ "_id" : ObjectId("4df08586188e444d001a763b"), "AccountID" : 2, "UserName" : "lb", "Password" : "1", "Age" : 25, "Email" : "libing@163.com", "RegisterDate" : "2011-06-09 16:36:95" }
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue