张心元 發表於 2019-10-28 15:43:00

【MongoDB详细使用教程】二、MongoDB基本操作

<p>【MongoDB详细使用教程】一、Mac安装MongoDB<br>
【MongoDB详细使用教程】二、MongoDB基本操作<br>
【MongoDB详细使用教程】三、高级查询<br>
【MongoDB详细使用教程】四、python操作MongoDB<br>
【MongoDB详细使用教程】五、MongoDB的数据库管理</p>
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>1、数据类型</li><li>2、数据库操作</li><li>3、集合操作</li><li>4、数据操作<ul><li>4.1、增</li><li>4.2、查</li><li>4.3、改<ul><li>4.3.1、修改整行</li><li>4.3.2、修改指定字段的值</li></ul></li><li>4.4、删</li></ul></li></ul></div><p></p>
<h1 id="1数据类型">1、数据类型</h1>
<table>
<thead>
<tr>
<th>MongoDB常见类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>Object ID</td>
<td>文档ID</td>
</tr>
<tr>
<td>String</td>
<td>字符串,最常用,必须是有效的UTF-8</td>
</tr>
<tr>
<td>Boolean</td>
<td>存储一个布尔值,true或false</td>
</tr>
<tr>
<td>Integer</td>
<td>整数可以是32位或64位,这取决于服务器</td>
</tr>
<tr>
<td>Double</td>
<td>存储浮点值</td>
</tr>
<tr>
<td>Arrays</td>
<td>数组(js)或列表(python),多个值存储到一个键</td>
</tr>
<tr>
<td>Object</td>
<td>用于嵌入式的文档,即一个值为一个文档</td>
</tr>
<tr>
<td>Null</td>
<td>存储Null值</td>
</tr>
<tr>
<td>Timestamp</td>
<td>时间戳</td>
</tr>
<tr>
<td>Date</td>
<td>存储当前日期或时间的UNIX时间格式</td>
</tr>
</tbody>
</table>
<h1 id="2数据库操作">2、数据库操作</h1>
<table>
<thead>
<tr>
<th>命令</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>show dbs</td>
<td>显示数据库列表</td>
</tr>
<tr>
<td>db</td>
<td>显示当前数据库</td>
</tr>
<tr>
<td>use 数据库名</td>
<td>切换或创建数据(有则切换,无则创建)</td>
</tr>
<tr>
<td>db.dropDatabase()</td>
<td>删除当前所在数据库(D大写)</td>
</tr>
</tbody>
</table>
<p>注:</p>
<ul>
<li>使用"show dbs"时,不显示空的数据库</li>
</ul>
<pre><code class="language-py">&gt; use mymongo
switched to db mymongo

&gt; db
mymongo

&gt; show dbs          # 创建后使用show dbs不会显示刚创建的数据库
admin   0.000GB
config0.000GB
local   0.000GB

&gt; db
mymongo

&gt; db.mytest.insert({"name":"chen"})
WriteResult({ "nInserted" : 1 })

&gt; show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
mymongo0.000GB    # 插入一条数据后,就可以在show dbs中显示出来了
</code></pre>
<ul>
<li>删除数据库前要切换到要删除的数据库,删除之后数据库内内容被清空,使用"show dbs"不显示,但是使用"db"命令的时候还会看到。</li>
</ul>
<pre><code>&gt; show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
mymongo0.000GB

&gt; db.dropDatabase()
{ "dropped" : "mymongo", "ok" : 1 }

&gt; show dbs
admin   0.000GB
config0.000GB
local   0.000GB
</code></pre>
<h1 id="3集合操作">3、集合操作</h1>
<p>关系型数据库中,每个库是由多张table组成,<br>
而NoSQL中,每个库是由多个集合(collection)组成,相当于sql中的table,集合中以键值对(json、python.dict)的形式保存数据。</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>db.createCollection(集合名, [参数])</td>
<td>创建集合(一般不用这种方式而是在插入数据时自动创建)</td>
</tr>
<tr>
<td>show collections/show tables</td>
<td>查看集合</td>
</tr>
<tr>
<td>db.集合名.drop()</td>
<td>删除集合</td>
</tr>
</tbody>
</table>
<pre><code>&gt; db.createCollection("colletiontest")
{ "ok" : 1 }

&gt; show collections
colletiontest
mytest

&gt; db.colletiontest.drop()
true

&gt; show collections
mytest
</code></pre>
<h1 id="4数据操作">4、数据操作</h1>
<p>也叫文档操作</p>
<h2 id="41增">4.1、增</h2>
<pre><code>db.集合名.insert({"键名1":值1, "键名2": 值2 ...})
</code></pre>
<pre><code>&gt; db.students.insert({"name":"chen","age":"18", "grade":"一年级"})
WriteResult({ "nInserted" : 1 })
&gt; db.students.insert({"name":"wang","age":"19", "grade":"二年级"})
WriteResult({ "nInserted" : 1 })
&gt; db.students.insert({"name":"xu","age":20, "grade":"三年级"})
WriteResult({ "nInserted" : 1 })

</code></pre>
<p>一条insert语句只能插入一行数据,insert后面不能跟多行数据。</p>
<h2 id="42查">4.2、查</h2>
<pre><code>db.集合名.findOne()      # 查询一行
db.集合名.find()         # 查询全部
db.集合名.find().pretty()# 格式化打印

db.集合名.find({查找条件}) # 按条件查找
</code></pre>
<pre><code>&gt; db.students.findOne()
{
        "_id" : ObjectId("5db63d020f98841018f7695f"),
        "name" : "chen",
        "age" : "18",
        "grade" : "一年级"
}

&gt; db.students.find()
{ "_id" : ObjectId("5db642b30f98841018f76965"), "name" : "chen", "age" : "18", "grade" : "一年级" }
{ "_id" : ObjectId("5db642bc0f98841018f76966"), "name" : "wang", "age" : "19", "grade" : "二年级" }
{ "_id" : ObjectId("5db653920f98841018f7696b"), "name" : "xu", "age" : 20, "grade" : "三年级" }

&gt; db.students.find().pretty()
{
        "_id" : ObjectId("5db642b30f98841018f76965"),
        "name" : "chen",
        "age" : "18",
        "grade" : "一年级"
}
{
        "_id" : ObjectId("5db642bc0f98841018f76966"),
        "name" : "wang",
        "age" : "19",
        "grade" : "二年级"
}
{
        "_id" : ObjectId("5db653920f98841018f7696b"),
        "name" : "xu",
        "age" : 20,
        "grade" : "三年级"
}
</code></pre>
<pre><code>&gt; db.students.find()
{ "_id" : ObjectId("5db642b30f98841018f76965"), "name" : "chen", "age" : "18", "grade" : "一年级" }
{ "_id" : ObjectId("5db642bc0f98841018f76966"), "name" : "wang", "age" : "19", "grade" : "二年级" }
{ "_id" : ObjectId("5db653920f98841018f7696b"), "name" : "xu", "age" : 20, "grade" : "三年级" }
{ "_id" : ObjectId("5db654660f98841018f7696c"), "name" : "ma", "age" : 20, "grade" : "二年级" }

&gt; db.students.find({"age":20})
{ "_id" : ObjectId("5db653920f98841018f7696b"), "name" : "xu", "age" : 20, "grade" : "三年级" }
{ "_id" : ObjectId("5db654660f98841018f7696c"), "name" : "ma", "age" : 20, "grade" : "二年级" }
</code></pre>
<p>"_id"是mongoDB自动添加的主键.<br>
十六进制,每两个字符为一个字节,共12个字节</p>
<ul>
<li>前4个字节为当前时间戳</li>
<li>接下来3个字节为机器ID</li>
<li>再接下来2个是MongoDB的进程ID</li>
<li>最后是排序流水号</li>
</ul>
<h2 id="43改">4.3、改</h2>
<h3 id="431修改整行">4.3.1、修改整行</h3>
<pre><code>db.集合名.update({查询条件}, {修改后结果})
</code></pre>
<p><img src="https://img2018.cnblogs.com/blog/1845117/201910/1845117-20191028154110590-1986103726.png" alt="" loading="lazy"></p>
<p>这里要注意,修改后结果就是整行的最终结果,并且只会修改一行,而不是所有匹配结果都修改。</p>
<p>可以通过多个字段查找<br>
<img src="https://img2018.cnblogs.com/blog/1845117/201910/1845117-20191028154127179-1211192050.png" alt="" loading="lazy"></p>
<h3 id="432修改指定字段的值">4.3.2、修改指定字段的值</h3>
<pre><code>db.students.update({查找条件}, {$set:{"要修改的字段名1":修改后的值, "要修改的字段名2": "值2"}})
</code></pre>
<p><img src="https://img2018.cnblogs.com/blog/1845117/201910/1845117-20191028154146865-1992754577.png" alt="" loading="lazy"></p>
<p>同时修改了连个字段的值,但同样只会修改一条记录</p>
<h2 id="44删">4.4、删</h2>
<pre><code>db.集合名.remove({查询条件})
db.集合名.remove({})    # 删除全部数据
</code></pre>
<p><img src="https://img2018.cnblogs.com/blog/1845117/201910/1845117-20191028154201725-1185959069.png" alt="" loading="lazy"></p>
<p>会删除所有的匹配项</p><br><br>
来源:https://www.cnblogs.com/cbowen/p/11752733.html
頁: [1]
查看完整版本: 【MongoDB详细使用教程】二、MongoDB基本操作