{"id":293,"date":"2014-01-29T00:52:14","date_gmt":"2014-01-28T15:52:14","guid":{"rendered":"http:\/\/tech.akat.info\/?p=293"},"modified":"2014-09-16T01:13:46","modified_gmt":"2014-09-15T16:13:46","slug":"mongodb","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=293","title":{"rendered":"MongoDB"},"content":{"rendered":"<h2>\u7279\u5fb4<\/h2>\n<p>\u30fbBSON(JSON\u3092\u30d0\u30a4\u30ca\u30ea\u5316)\u5f62\u5f0f\u3067\u4fdd\u5b58\u3057\u305f\u3082\u306e\u3092value\u3068\u3057\u3066\u3001\u7279\u5b9a\u306ekey\u306b\u7d10\u3065\u3051\u308b<br \/>\n\u30fb\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9(\u30b9\u30ad\u30fc\u30de\u30ec\u30b9\u3001\u30ab\u30e9\u30e0\u304c\u56fa\u5b9a\u3055\u308c\u306a\u3044)<br \/>\n(\u30c6\u30fc\u30d6\u30eb\u306e\u3053\u3068\u3092\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3001\u30ec\u30b3\u30fc\u30c9\u306e\u3053\u3068\u3092\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3068\u547c\u3076)<br \/>\n\u30fbJOIN\u3068\u4f3c\u305f\u3088\u3046\u306a\u6a5f\u80fd\u306eembed\u3068\u3044\u3046\u6a5f\u69cb\u304c\u5b58\u5728\u3059\u308b<br \/>\n\u30fb\u9ad8\u901f\u3001\u304b\u3064\u30c9\u30e9\u30a4\u30d0\u304c\u8c4a\u5bcc\u306b\u7528\u610f\u3055\u308c\u3066\u3044\u308b<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u64cd\u4f5c\u65b9\u6cd5<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@akat:\/home\/akat# aptitude install mongodb\r\nroot@akat:\/home\/akat# mongo # mongo\u30b7\u30a7\u30eb\u306e\u8d77\u52d5\r\nMongoDB shell version: 2.0.6\r\nconnecting to: test\r\n&gt; help\r\n        db.help()                    help on db methods\r\n        db.mycoll.help()             help on collection methods\r\n        rs.help()                    help on replica set methods\r\n        help admin                   administrative help\r\n        help connect                 connecting to a db help\r\n        help keys                    key shortcuts\r\n        help misc                    misc things to know\r\n        help mr                      mapreduce\r\n\r\n        show dbs                     show database names\r\n        show collections             show collections in current database\r\n        show users                   show users in current database\r\n        show profile                 show most recent system.profile entries with time &gt;= 1ms\r\n        show logs                    show the accessible logger names\r\n        show log &#x5B;name]              prints out the last segment of log in memory, 'global' is default\r\n        use &lt;db_name&gt;                set current database\r\n        db.foo.find()                list objects in collection foo\r\n        db.foo.find( { a : 1 } )     list objects in foo where a == 1\r\n        it                           result of the last line evaluated; use to further iterate\r\n        DBQuery.shellBatchSize = x   set default number of items to display on shell\r\n        exit                         quit the mongo shell\r\n&gt; show dbs # db\u306e\u8868\u793a\r\nlocal   (empty)\r\ntest    0.203125GB\r\n&gt; db.local.save({name:&quot;akatuki&quot;}) # db\u3078\u306e\u767b\u9332\r\n&gt; db.local.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;52e7ceb6c54ecea55b6be1b7&quot;), &quot;name&quot; : &quot;akatuki&quot; } # _id\u3068\u3044\u3046\u30e6\u30cb\u30fc\u30af\u306a\u5024\u3092\u6301\u3063\u3066\u3044\u308b\r\n&gt; use blog_app; # use\u3067DB\u304c\u3067\u304d\u305f\u3088\u3046\u306b\u898b\u3048\u308b\u304c\u3001\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3084\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u767b\u9332\u3059\u308b\u307e\u3067\u306f\u8a8d\u8b58\u3055\u308c\u306a\u3044\r\nswitched to db blog_app\r\n&gt; show dbs;\r\nlocal   (empty)\r\ntest    0.203125GB\r\n&gt; db.createCollection(&quot;posts&quot;) # \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\r\n{ &quot;ok&quot; : 1 }\r\n&gt; show dbs;\r\nblog_app        0.203125GB\r\nlocal   (empty)\r\ntest    0.203125GB\r\n&gt; db.help # \u5229\u7528\u3067\u304d\u308b\u95a2\u6570\u3092\u8868\u793a\u3059\u308b\r\nfunction () {\r\n    print(&quot;DB methods:&quot;);\r\n    print(&quot;\\tdb.addUser(username, password&#x5B;, readOnly=false])&quot;);\r\n    print(&quot;\\tdb.auth(username, password)&quot;);\r\n    print(&quot;\\tdb.cloneDatabase(fromhost)&quot;);\r\n    print(&quot;\\tdb.commandHelp(name) returns the help for the command&quot;);\r\n    print(&quot;\\tdb.copyDatabase(fromdb, todb, fromhost)&quot;);\r\n    print(&quot;\\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )&quot;);\r\n    print(&quot;\\tdb.currentOp() displays the current operation in the db&quot;);\r\n    # \u9577\u3044\u306e\u3067\u7701\u7565\r\n&gt; db.stats(); # DB\u306e\u72b6\u614b\u3092\u8868\u793a\r\n{\r\n        &quot;db&quot; : &quot;blog_app&quot;,\r\n        &quot;collections&quot; : 3,\r\n        &quot;objects&quot; : 4,\r\n        &quot;avgObjSize&quot; : 58,\r\n        &quot;dataSize&quot; : 232,\r\n        &quot;storageSize&quot; : 20480,\r\n        &quot;numExtents&quot; : 3,\r\n        &quot;indexes&quot; : 1,\r\n        &quot;indexSize&quot; : 8176,\r\n        &quot;fileSize&quot; : 201326592,\r\n        &quot;nsSizeMB&quot; : 16,\r\n        &quot;ok&quot; : 1\r\n}\r\n&gt; db.dropDatabase(); # DB\u306e\u524a\u9664\r\n{ &quot;dropped&quot; : &quot;blog_app&quot;, &quot;ok&quot; : 1 }\r\n&gt; use blog_app; # \u518d\u5ea6blog_app\u3092\u4f5c\u6210\r\nswitched to db blog_app\r\n&gt; db.createCollection(&quot;posts&quot;); # \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\r\n{ &quot;ok&quot; : 1 }\r\n&gt; db.createCollection(&quot;users&quot;);\r\n{ &quot;ok&quot; : 1 }\r\n&gt; show collections;\r\nposts\r\nsystem.indexes\r\nusers\r\n&gt; db.users.drop(); # users\u3092\u524a\u9664\r\ntrue\r\n&gt; db.posts.renameCollection(&quot;entries&quot;); # \u540d\u524d\u3092\u5909\u66f4\u3059\u308b\r\n{ &quot;ok&quot; : 1 }\r\n&gt; show collections;\r\nentries\r\nsystem.indexes\r\n&gt; db.entries.insert({&quot;name&quot;:&quot;akat&quot;,&quot;email&quot;:&quot;akat@gmail.com&quot;}); # \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306binsert\u3059\u308b\r\n&gt; db.entries.find();\r\n{ &quot;_id&quot; : ObjectId(&quot;530045ff860d11e5c1173c77&quot;), &quot;name&quot; : &quot;akat&quot;, &quot;email&quot; : &quot;akat@gmail.com&quot; }\r\n&gt; db.entries.insert({&quot;name&quot;:&quot;akatuki&quot;,&quot;lang&quot;:&#x5B;&quot;JP&quot;,&quot;US&quot;]});\r\n&gt; db.entries.find(); # \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8868\u793a\u3059\u308b\r\n{ &quot;_id&quot; : ObjectId(&quot;530045ff860d11e5c1173c77&quot;), &quot;name&quot; : &quot;akat&quot;, &quot;email&quot; : &quot;akat@gmail.com&quot; }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004634860d11e5c1173c78&quot;), &quot;name&quot; : &quot;akatuki&quot;, &quot;lang&quot; : &#x5B; &quot;JP&quot;, &quot;US&quot; ] }\r\n&gt; db.entries.remove(); # \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u524a\u9664\u3059\u308b\r\n&gt; db.entries.find();\r\n&gt; for (var i=0;i&lt;10;i++){\r\n... db.users.insert(\r\n... {&quot;name&quot;:&quot;user-&quot;+i,\r\n... &quot;team&quot;:i %3,\r\n... &quot;score&quot;:Math.floor(Math.random()*100)\r\n... }\r\n... );\r\n... } # JavaScript\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c84&quot;), &quot;name&quot; : &quot;user-1&quot;, &quot;team&quot; : 1, &quot;score&quot; : 24 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c86&quot;), &quot;name&quot; : &quot;user-3&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c87&quot;), &quot;name&quot; : &quot;user-4&quot;, &quot;team&quot; : 1, &quot;score&quot; : 33 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c88&quot;), &quot;name&quot; : &quot;user-5&quot;, &quot;team&quot; : 2, &quot;score&quot; : 72 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c89&quot;), &quot;name&quot; : &quot;user-6&quot;, &quot;team&quot; : 0, &quot;score&quot; : 19 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8a&quot;), &quot;name&quot; : &quot;user-7&quot;, &quot;team&quot; : 1, &quot;score&quot; : 46 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;name&quot; : &quot;user-9&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n&gt; db.users.find({&quot;team&quot;:0}); # team\u304c0\u3067\u3042\u308b\u3082\u306e\u3092\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c86&quot;), &quot;name&quot; : &quot;user-3&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c89&quot;), &quot;name&quot; : &quot;user-6&quot;, &quot;team&quot; : 0, &quot;score&quot; : 19 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;name&quot; : &quot;user-9&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n&gt; db.users.find({&quot;team&quot;:0},{&quot;name&quot;:true}); # team\u304c0\u3067\u3042\u308b\u3082\u306e\u3067\u3001name\u3060\u3051\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot; }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c86&quot;), &quot;name&quot; : &quot;user-3&quot; }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c89&quot;), &quot;name&quot; : &quot;user-6&quot; }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;name&quot; : &quot;user-9&quot; }\r\n&gt; db.users.find({&quot;team&quot;:{$ne:0}}); # team\u304c0\u3067\u306a\u3044\u3082\u306e\u3092\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c84&quot;), &quot;name&quot; : &quot;user-1&quot;, &quot;team&quot; : 1, &quot;score&quot; : 24 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c87&quot;), &quot;name&quot; : &quot;user-4&quot;, &quot;team&quot; : 1, &quot;score&quot; : 33 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c88&quot;), &quot;name&quot; : &quot;user-5&quot;, &quot;team&quot; : 2, &quot;score&quot; : 72 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8a&quot;), &quot;name&quot; : &quot;user-7&quot;, &quot;team&quot; : 1, &quot;score&quot; : 46 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n&gt; db.users.find({&quot;score&quot;:{$gt:40}}); # score\u304c40\u3088\u308a\u5927\u304d\u3044\u3082\u306e\u3092\u62bd\u51fa(\u4ee5\u4e0a\u306e\u3068\u304d\u306f$gte)\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c88&quot;), &quot;name&quot; : &quot;user-5&quot;, &quot;team&quot; : 2, &quot;score&quot; : 72 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8a&quot;), &quot;name&quot; : &quot;user-7&quot;, &quot;team&quot; : 1, &quot;score&quot; : 46 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n&gt; db.users.find({&quot;score&quot;:{$gt:40,$lt:60}}); # score\u304c40\u3088\u308a\u5927\u304d\u304f\u300160\u672a\u6e80\u3092\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8a&quot;), &quot;name&quot; : &quot;user-7&quot;, &quot;team&quot; : 1, &quot;score&quot; : 46 }\r\n&gt; db.users.find({&quot;name&quot;:{$regex:\/user-&#x5B;0-2]\/i}}); # \u6b63\u898f\u8868\u73fe\u306b\u3066\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c84&quot;), &quot;name&quot; : &quot;user-1&quot;, &quot;team&quot; : 1, &quot;score&quot; : 24 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n&gt; db.users.find().sort({&quot;score&quot;:1}) # score\u306b\u3064\u3044\u3066\u6607\u9806\u306b\u4e26\u3079\u308b(\u964d\u9806\u306f-1)\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c89&quot;), &quot;name&quot; : &quot;user-6&quot;, &quot;team&quot; : 0, &quot;score&quot; : 19 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c84&quot;), &quot;name&quot; : &quot;user-1&quot;, &quot;team&quot; : 1, &quot;score&quot; : 24 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c87&quot;), &quot;name&quot; : &quot;user-4&quot;, &quot;team&quot; : 1, &quot;score&quot; : 33 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c86&quot;), &quot;name&quot; : &quot;user-3&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;name&quot; : &quot;user-9&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8a&quot;), &quot;name&quot; : &quot;user-7&quot;, &quot;team&quot; : 1, &quot;score&quot; : 46 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c88&quot;), &quot;name&quot; : &quot;user-5&quot;, &quot;team&quot; : 2, &quot;score&quot; : 72 }\r\n&gt; db.users.find().limit(3) # 3\u4ef6\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c84&quot;), &quot;name&quot; : &quot;user-1&quot;, &quot;team&quot; : 1, &quot;score&quot; : 24 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n&gt; db.users.find().skip(2).limit(3) # 2\u4ef6\u98db\u3070\u3057\u3066\u30013\u4ef6\u62bd\u51fa\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c85&quot;), &quot;name&quot; : &quot;user-2&quot;, &quot;team&quot; : 2, &quot;score&quot; : 51 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c86&quot;), &quot;name&quot; : &quot;user-3&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c87&quot;), &quot;name&quot; : &quot;user-4&quot;, &quot;team&quot; : 1, &quot;score&quot; : 33 }\r\n&gt; db.users.find().count() # \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u6570\u3092\u62bd\u51fa\r\n10\r\n&gt; db.users.distinct(&quot;team&quot;) # \u30e6\u30cb\u30fc\u30af\u306a\u5024\u3092\u62bd\u51fa\r\n&#x5B; 0, 1, 2 ]\r\n&gt; db.users.getIndexKeys(); # index\u60c5\u5831\u3092\u8868\u793a\r\n&#x5B; { &quot;_id&quot; : 1 } ]\r\n&gt; db.users.ensureIndex({&quot;score&quot;:1}) # index\u3092\u4f5c\u6210\r\n&gt; db.users.getIndexKeys();\r\n&#x5B; { &quot;_id&quot; : 1 }, { &quot;score&quot; : 1 } ]\r\n&gt; db.users.dropIndex({&quot;score&quot;:1}) # index\u3092\u524a\u9664\r\n{ &quot;nIndexesWas&quot; : 2, &quot;ok&quot; : 1 }\r\n&gt; db.users.getIndexKeys();\r\n&#x5B; { &quot;_id&quot; : 1 } ]\r\n&gt; db.users.ensureIndex({&quot;name&quot;:1},{&quot;unique&quot;:true}) # name\u306b\u3064\u3044\u3066unique\u306e\u307f\u8a31\u53ef\u3059\u308b\r\n&gt; db.users.getIndexKeys();\r\n&#x5B; { &quot;_id&quot; : 1 }, { &quot;name&quot; : 1 } ]\r\n&gt; db.users.find().limit(1)\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c83&quot;), &quot;name&quot; : &quot;user-0&quot;, &quot;team&quot; : 0, &quot;score&quot; : 28 }\r\n&gt; db.users.insert({&quot;name&quot; : &quot;user-0&quot;}) # \u5b58\u5728\u3059\u308b\u5024\u3092\u633f\u5165\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\r\nE11000 duplicate key error index: blog_app.users.$name_1  dup key: { : &quot;user-0&quot; }\r\n&gt; db.users.remove({&quot;name&quot;:{$regex:\/user-&#x5B;0-7]\/i}}) # \u6b63\u898f\u8868\u73fe\u3092\u5229\u7528\u3057\u3066\u524a\u9664\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;name&quot; : &quot;user-9&quot;, &quot;team&quot; : 0, &quot;score&quot; : 40 }\r\n&gt; db.users.update({&quot;name&quot;:&quot;user-9&quot;},{&quot;score&quot; : 100}) # score\u306e\u307f\u3057\u304b\u6b8b\u3089\u306a\u3044\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 61 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;score&quot; : 100 }\r\n&gt; db.users.update({&quot;name&quot;:&quot;user-8&quot;},{$set:{&quot;score&quot; : 100}}) # \u4ed6\u306e\u30ab\u30e9\u30e0\u3082\u6b8b\u3059\u5834\u5408\u306f\u3001$set\u3092\u5229\u7528\u3059\u308b\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 100 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;score&quot; : 100 }\r\n&gt; db.users.update({&quot;name&quot;:&quot;user-8&quot;},{$inc:{&quot;score&quot; : -20}}) # score\u309220\u6e1b\u3089\u3059\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2, &quot;score&quot; : 80 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;score&quot; : 100 }\r\n&gt; db.users.update({&quot;name&quot;:&quot;user-8&quot;},{$unset:{&quot;score&quot; : 1}}) # \u30ab\u30e9\u30e0\u3092\u524a\u9664\u3059\u308b\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;name&quot; : &quot;user-8&quot;, &quot;team&quot; : 2 }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;score&quot; : 100 }\r\n&gt; db.users.update({&quot;name&quot;:&quot;user-8&quot;},{$rename:{&quot;team&quot;:&quot;myteam&quot;}}) # \u30ab\u30e9\u30e0\u540d\u3092\u5909\u66f4\u3059\u308b\r\n&gt; db.users.find()\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8b&quot;), &quot;myteam&quot; : 2, &quot;name&quot; : &quot;user-8&quot; }\r\n{ &quot;_id&quot; : ObjectId(&quot;53004852860d11e5c1173c8c&quot;), &quot;score&quot; : 100 }\r\n&gt; exit\r\nbye\r\nroot@akat:\/home\/akat# mkdir mongo\r\nroot@akat:\/home\/akat# chown nobody:nogroup mongo\/\r\nroot@akat:\/home\/akat# mongod --dbpath mongo --port 11111 # 11111port\u3067\u8d77\u52d5\r\n<\/pre>\n<h2>\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u3001\u30ea\u30b9\u30c8\u30a2<\/h2>\n<p>mongodump\u306b\u3064\u3044\u3066\u8a18\u8f09<br \/>\n\u25a0\u30e1\u30ea\u30c3\u30c8<br \/>\n\u30fb\u30aa\u30f3\u30e9\u30a4\u30f3\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u304c\u53ef\u80fd<br \/>\n\u30fb\u30c7\u30fc\u30bf\u3092\u30d0\u30a4\u30ca\u30ea\uff08BSON\uff09\u5f62\u5f0f\u3067\u30c0\u30f3\u30d7\u3059\u308b\u305f\u3081\u3001\u30d5\u30a1\u30a4\u30eb\u30b5\u30a4\u30ba\u3092\u6bd4\u8f03\u7684\u5c0f\u3055\u304f\u6291\u3048\u3089\u308c\u308b<\/p>\n<p>\u25a0\u30c7\u30e1\u30ea\u30c3\u30c8<br \/>\n\u30fb\u30c7\u30fc\u30bf\u3092\u30d0\u30a4\u30ca\u30ea\uff08BSON\uff09\u5f62\u5f0f\u3067\u30c0\u30f3\u30d7\u3059\u308b\u305f\u3081\u3001\u30c7\u30fc\u30bf\u306e\u5909\u63db\u306b\u6642\u9593\u304c\u304b\u304b\u308b<br \/>\n\u30fb\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u30c0\u30f3\u30d7\u3055\u308c\u305a\u306b\u30ea\u30b9\u30c8\u30a2\u6642\u306b\u518d\u69cb\u7bc9\u3055\u308c\u308b\u305f\u3081\u3001\u5927\u304d\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u3042\u308b\u5834\u5408\u306b\u306f\u30ea\u30b9\u30c8\u30a2\u306b\u6642\u9593\u304c\u304b\u304b\u308b<br \/>\n\u30fb\u5c0f\u898f\u6a21\u904b\u7528\u3092\u524d\u63d0\u306b\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b(\u30c7\u30fc\u30bf\u3092\u4e00\u7b87\u6240\u306b\u4fdd\u5b58\u3059\u308b\u3001\u5b9f\u884c\u4e2d\u306f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3059\u308b)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@akat:\/home\/akat# mongodump --db blog_app # json\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u51fa\u529b\u3059\u308b\u3001dump\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u4f5c\u6210\u3055\u308c\u308b\r\nconnected to: 127.0.0.1\r\nDATABASE: blog_app       to     dump\/blog_app\r\n        blog_app.system.indexes to dump\/blog_app\/system.indexes.bson\r\n                 3 objects\r\n        blog_app.entries to dump\/blog_app\/entries.bson\r\n                 0 objects\r\n        blog_app.users to dump\/blog_app\/users.bson\r\n                 10 objects\r\nroot@akat:\/home\/akat# mongo\r\nMongoDB shell version: 2.0.6\r\nconnecting to: test\r\n&gt; show dbs\r\nadmin   (empty)\r\nblog_app        0.203125GB\r\nlocal   (empty)\r\ntest    0.203125GB\r\n&gt; use blog_app # db\u3092\u524a\u9664\u3059\u308b\r\nswitched to db blog_app\r\n&gt; db.dropDatabase()\r\n{ &quot;dropped&quot; : &quot;blog_app&quot;, &quot;ok&quot; : 1 }\r\n&gt; show dbs\r\nadmin   (empty)\r\nlocal   (empty)\r\ntest    0.203125GB\r\n&gt; exit\r\nbye\r\nroot@akat:\/home\/akat# mongorestore # dump\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e2d\u3092\u898b\u3066\u3001\u30ea\u30b9\u30c8\u30a2\u3059\u308b\r\nconnected to: 127.0.0.1\r\nSun Feb 16 22:21:18 dump\/blog_app\/entries.bson\r\nSun Feb 16 22:21:18      going into namespace &#x5B;blog_app.entries]\r\nSun Feb 16 22:21:18 file dump\/blog_app\/entries.bson empty, skipping\r\nSun Feb 16 22:21:18 dump\/blog_app\/users.bson\r\nSun Feb 16 22:21:18      going into namespace &#x5B;blog_app.users]\r\n10 objects found\r\nSun Feb 16 22:21:18 dump\/blog_app\/system.indexes.bson\r\nSun Feb 16 22:21:18      going into namespace &#x5B;blog_app.system.indexes]\r\nSun Feb 16 22:21:18 { key: { _id: 1 }, ns: &quot;blog_app.entries&quot;, name: &quot;_id_&quot; }\r\nSun Feb 16 22:21:19 { key: { _id: 1 }, ns: &quot;blog_app.users&quot;, name: &quot;_id_&quot; }\r\nSun Feb 16 22:21:19 { key: { name: 1.0 }, unique: true, ns: &quot;blog_app.users&quot;, name: &quot;name_1&quot; }\r\n3 objects found\r\nroot@akat:\/home\/akat# mongo\r\nMongoDB shell version: 2.0.6\r\nconnecting to: test\r\n&gt; show dbs\r\nadmin   (empty)\r\nblog_app        0.203125GB\r\nlocal   (empty)\r\ntest    0.203125GB\r\n<\/pre>\n<h2>\u53c2\u8003<\/h2>\n<p>http:\/\/dotinstall.com\/lessons\/basic_mongodb<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7279\u5fb4 \u30fbBSON(JSON\u3092\u30d0\u30a4\u30ca\u30ea\u5316)\u5f62\u5f0f\u3067\u4fdd\u5b58\u3057\u305f\u3082\u306e\u3092value\u3068\u3057\u3066\u3001\u7279\u5b9a\u306ekey\u306b\u7d10\u3065\u3051\u308b \u30fb\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9(\u30b9\u30ad\u30fc\u30de\u30ec\u30b9\u3001\u30ab\u30e9\u30e0\u304c\u56fa\u5b9a\u3055\u308c\u306a\u3044) (\u30c6\u30fc\u30d6\u30eb\u306e\u3053\u3068\u3092\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3001\u30ec\u30b3\u30fc\u30c9\u306e\u3053\u3068\u3092\u30c9\u30ad [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/293"}],"collection":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=293"}],"version-history":[{"count":13,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}