MongoDB常用命令
<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.38889"><p align="left"><strong><span style="font-size: 12pt; font-family: 宋体; color: rgba(255, 255, 255, 1); background: rgba(6, 129, 186, 1)">MongoDB</span></strong><strong><span style="font-size: 12pt; font-family: 宋体; color: rgba(255, 255, 255, 1); background: rgba(6, 129, 186, 1)">的基本操作</span></strong></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">Mongodb</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">中关键字种类:</span></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">db</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">(数据库实例级别)</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> db</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">本身</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> db.connection </span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">数据库下的集合信息</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> db.collection.xxx(</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">rs</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">(复制集级别)</span></em></p><p align="left"><em> </em></p><p align="left"><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">sh</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">(分片级别)</span></em></p><p align="left" style="text-indent: 21pt"><strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(255, 0, 0, 1)">rs.status() </span></strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">#</span><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">副本集状态,一个primary,其它SECONDARY。primary是主,只有primary能写入,从库无法插入数据</span></p><p align="left" style="text-indent: 21pt"><strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(223, 64, 42, 1)">rs.config() </span></strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">##</span><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">获取配置</span></p><p align="left" style="text-indent: 21pt"><strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(255, 0, 0, 1)">rs.slaveOk() </span></strong><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">#SECONDARY</span><span style="font-size: 12pt; font-family: "微软雅黑", "sans-serif"; color: rgba(46, 116, 181, 1)">需要声明是slave才能查看数据</span></p><p align="left" style="text-indent: 21pt"> </p><p align="left"><strong><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">查询操作</span></strong></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">在客户端指定数据库进行连接:(默认连接本机</span><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">test</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">数据库)</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">$ mongo <span style="color: rgba(128, 0, 128, 1)">10.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.152</span>/admin MongoDB shell version: <span style="color: rgba(128, 0, 128, 1)">3.2</span>.<span style="color: rgba(128, 0, 128, 1)">8</span> connecting to: <span style="color: rgba(128, 0, 128, 1)">10.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.152</span>/admin </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db admin</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查看当前数据库版本</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.version() </span></p><p align="left"> </p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">切换数据库</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> use test; switched to db test</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">显示当前数据库</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db test > db.getName() test</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查询所有数据库</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; color: rgba(128, 128, 128, 1); background: rgba(249, 249, 249, 1)">></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> show dbs; smile <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB local <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB test <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB <span style="color: rgba(128, 128, 128, 1)">></span> show databases; smile <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB local <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB test <strong><span style="color: rgba(128, 0, 0, 1)">0</span></strong>.000GB</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">查看</span><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">smile</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">数据库当前状态</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> <span style="color: rgba(0, 0, 255, 1)">use</span> smile; </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> </span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(195, 215, 152, 1)">db.stats()</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> { <span style="color: rgba(128, 0, 0, 1)">"db"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 0, 1)">"smile"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"collections"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">1</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"objects"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">10000</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"avgObjSize"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">80</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"dataSize"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">800000</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"storageSize"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">258048</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"numExtents"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">0</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"indexes"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">1</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"indexSize"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">94208</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"ok"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">1</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> }</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">查看当前数据库的连接机器地址</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> </span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(195, 215, 152, 1)">db.getMongo()</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> connection to 127.0.0.1</span></p><p align="left"><strong><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">1.5.2 </span></strong><strong><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">数据管理</span></strong></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">创建数据库</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> use smile;</span></p><p align="left"><strong><em><u><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">说明:</span></u></em></strong></p><p align="left"><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">创建数据库:</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">当</span></em><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">use</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">的时候,系统就会自动创建一个数据库。</span></em></p><p align="left"><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">如果</span></em><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">use</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">之后没有创建任何集合。系统就会删除这个数据库。</span></em></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">删除数据库</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> show dbs; smile 0.000GB local 0.000GB test 0.000GB </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> use smile switched to db smile </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> </span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(195, 215, 152, 1)">db.dropDatabase()</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> { "dropped" : "smile", "ok" : 1 }</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">说明:</span></p><p align="left"><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">删除数据库:</span></em></p><p align="left"><em> </em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)"> 如果没有选择任何数据库,会删除默认的</span></em><em><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">test</span></em><em><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">数据库</span></em></p><p align="left"><strong><span style="font-size: 12pt; font-family: 宋体; background: rgba(0, 255, 0, 1)">创建集合</span></strong></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">方法一:</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> use smile; switched to db smile </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> </span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(195, 215, 152, 1)">db.createCollection('a')</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> { "ok" : 1 } </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.createCollection('b') { "ok" : 1 }</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查看当前数据下的所有集合</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> show collections; a b </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.getCollectionNames() [ <span style="color: rgba(128, 0, 0, 1)">"a"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"b"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> ]</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">方法二:</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)"> 当插入一个文档的时候,一个集合就会自动创建。</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> <span style="color: rgba(0, 0, 255, 1)">use</span> smile; switched <span style="color: rgba(0, 0, 255, 1)">to</span> db smile </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.c.insert({name:<span style="color: rgba(128, 0, 0, 1)">'smile'</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">}); WriteResult({ <span style="color: rgba(128, 0, 0, 1)">"nInserted"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : <span style="color: rgba(128, 0, 128, 1)">1</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> }) </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.c.insert({url:</span><span style="color: rgba(128, 0, 0, 1)">'http://blog.nmtui.com'</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">}); WriteResult({ </span><span style="color: rgba(128, 0, 0, 1)">"nInserted"</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> : </span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> })</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">查看创建的合集</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.getCollectionNames() [ <span style="color: rgba(128, 0, 0, 1)">"a"</span>, <span style="color: rgba(128, 0, 0, 1)">"b"</span>, <span style="color: rgba(128, 0, 0, 1)">"c"</span> ]</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(195, 215, 152, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查看合集里的内容</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.c.find() { <span style="color: rgba(128, 0, 0, 1)">"_id"</span> : ObjectId(<span style="color: rgba(128, 0, 0, 1)">"5a4cbcea83ec78b7bea904f8"</span>), <span style="color: rgba(128, 0, 0, 1)">"name"</span> : <span style="color: rgba(128, 0, 0, 1)">"smile"</span> } { <span style="color: rgba(128, 0, 0, 1)">"_id"</span> : ObjectId(<span style="color: rgba(128, 0, 0, 1)">"5a4cbcfc83ec78b7bea904f9"</span>), <span style="color: rgba(128, 0, 0, 1)">"url"</span> : <span style="color: rgba(128, 0, 0, 1)">"http://blog.nmtui.com"</span> }</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">重命名集合</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.c.renameCollection(<span style="color: rgba(128, 0, 0, 1)">"smile"</span>) { <span style="color: rgba(128, 0, 0, 1)">"ok"</span> : <span style="color: rgba(128, 0, 128, 1)">1</span> } </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.getCollectionNames() [ <span style="color: rgba(128, 0, 0, 1)">"a"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"b"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"smile"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> ]</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(195, 215, 152, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">删除合集</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.a.drop() <span style="color: rgba(0, 0, 255, 1)">true</span> </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.getCollectionNames() [ <span style="color: rgba(128, 0, 0, 1)">"b"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">, <span style="color: rgba(128, 0, 0, 1)">"smile"</span></span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> ]</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)"> </span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">插入</span><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">1w</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">行数据</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> <span style="color: rgba(0, 0, 255, 1)">for</span>(i=<span style="color: rgba(128, 0, 128, 1)">0</span>;i<<span style="color: rgba(128, 0, 128, 1)">10000</span>;i++){ db.log.insert({<span style="color: rgba(128, 0, 0, 1)">"uid"</span>:i,<span style="color: rgba(128, 0, 0, 1)">"name"</span>:<span style="color: rgba(128, 0, 0, 1)">"mongodb"</span>,<span style="color: rgba(128, 0, 0, 1)">"age"</span>:<span style="color: rgba(128, 0, 128, 1)">6</span>,<span style="color: rgba(128, 0, 0, 1)">"date"</span>:<span style="color: rgba(0, 0, 255, 1)">new</span> Date()}); } WriteResult({ <span style="color: rgba(128, 0, 0, 1)">"nInserted"</span> : <span style="color: rgba(128, 0, 128, 1)">1</span> })</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查询集合中的查询所有记录</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.find()</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">注:默认每页显示</span><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">20</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">条记录,当显示不下的的情况下,可以用</span><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">it</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">迭代命令查询下一页数据。</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> DBQuery.shellBatchSize=<span style="color: rgba(128, 0, 128, 1)">50</span>; # </span><span style="font-size: 12pt; font-family: 宋体">每页显示</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">50</span><span style="font-size: 12pt; font-family: 宋体">条记录</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> <span style="color: rgba(128, 0, 128, 1)">50</span> app> db.log.findOne() # </span><span style="font-size: 12pt; font-family: 宋体">查看第</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">1</span><span style="font-size: 12pt; font-family: 宋体">条记录</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> app> db.log.count() # </span><span style="font-size: 12pt; font-family: 宋体">查询总的记录数</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> app> db.log.find({uid:<span style="color: rgba(128, 0, 128, 1)">1000</span>}); # </span><span style="font-size: 12pt; font-family: 宋体">查询</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">UUID</span><span style="font-size: 12pt; font-family: 宋体">为</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">1000</span><span style="font-size: 12pt; font-family: 宋体">的数据</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">删除集合中的记录数</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.distinct(<span style="color: rgba(128, 0, 0, 1)">"name"</span>) # </span><span style="font-size: 12pt; font-family: 宋体">查询去掉当前集合中某列的重复数据</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> [ <span style="color: rgba(128, 0, 0, 1)">"mongodb"</span> ] </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.remove({}) # </span><span style="font-size: 12pt; font-family: 宋体">删除集合中所有记录</span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> WriteResult({ <span style="color: rgba(128, 0, 0, 1)">"nRemoved" : </span><span style="color: rgba(128, 0, 0, 1)"> }) </span><span style="color: rgba(128, 0, 0, 1)">> db.log.distinct(</span></span><span style="color: rgba(128, 0, 0, 1)">"name"</span><span style="color: rgba(128, 0, 128, 1)">10000</span><span style="color: rgba(128, 0, 0, 1)">) [ ]</span></p><p align="left"><span style="font-size: 12pt; font-family: 宋体; background: rgba(195, 215, 152, 1)">查看集合存储信息</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.stats() # </span><span style="font-size: 12pt; font-family: 宋体">查看数据状态</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.dataSize() # </span><span style="font-size: 12pt; font-family: 宋体">集合中数据的原始大小</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.totalIndexSize() # </span><span style="font-size: 12pt; font-family: 宋体">集合中索引数据的原始大小</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.totalSize() # </span><span style="font-size: 12pt; font-family: 宋体">集合中索引</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">+</span><span style="font-size: 12pt; font-family: 宋体">数据压缩存储之后的大小</span><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)"> </span></p><p><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.storageSize() # </span><span style="font-size: 12pt; font-family: 宋体">集合中数据压缩存储的大小</span></p><p align="left"><span style="font-size: 12pt; font-family: "Verdana", "sans-serif"; background: rgba(255, 255, 255, 1)">pretty()</span><span style="font-size: 12pt; font-family: 宋体; background: rgba(255, 255, 255, 1)">使用</span></p><p align="left"><span style="font-size: 12pt; font-family: "Courier New"; background: rgba(249, 249, 249, 1)">> db.log.find({uid:<span style="color: rgba(128, 0, 128, 1)">1000</span>}).pretty() { <span style="color: rgba(128, 0, 0, 1)">"_id"</span> : ObjectId(<span style="color: rgba(128, 0, 0, 1)">"5a4c5c0bdf067ab57602f7c2"</span>), <span style="color: rgba(128, 0, 0, 1)">"uid"</span> : <span style="color: rgba(128, 0, 128, 1)">1000</span>, <span style="color: rgba(128, 0, 0, 1)">"name"</span> : <span style="color: rgba(128, 0, 0, 1)">"mongodb"</span>, <span style="color: rgba(128, 0, 0, 1)">"age"</span> : <span style="color: rgba(128, 0, 128, 1)">6</span>, <span style="color: rgba(128, 0, 0, 1)">"date"</span> : ISODate(<span style="color: rgba(128, 0, 0, 1)">"2018-01-03T04:28:59.343Z"</span>) }</span></p><p> </p></div><br><br>来源:https://www.cnblogs.com/--smile/p/11055204.html
頁:
[1]