0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mongodbに関してのメモ

Last updated at Posted at 2016-12-04

目的

ざっくりとMongoDBを把握したい

ダウンロード

気軽にCentOS6上で試したいのでbinaryをダウンロード。
https://www.mongodb.org/dl/linux/x86_64-rhel62

mongodbのファイル構成

$ tree .
.
├── GNU-AGPL-3.0
├── MPL-2
├── README
├── THIRD-PARTY-NOTICES
└── bin
    ├── bsondump
    ├── mongo
    ├── mongod
    ├── mongodump
    ├── mongoexport
    ├── mongofiles
    ├── mongoimport
    ├── mongooplog
    ├── mongoperf
    ├── mongorestore
    ├── mongos
    ├── mongosniff
    ├── mongostat
    └── mongotop

mongodの起動

$ mkdir -p ./data/db
$ ./bin/mongod -dbpath data/db

MongoDBのデータ構造

データベースはMySQLでいうところのデータベースで、Collectionはテーブル相当でしょうか。Documentは行相当だけどスキーマレスであるところがRDBとは違う。primary keyは"_id"あたり?

MongoDB/
├── Database1
│   ├── Collection1
│   │   ├── Document1
│   │   └── Document2
│   └── Collection2
│       ├── Document1
│       └── Document2
└── Database2
    ├── Collection1
    │   ├── Document1
    │   └── Document2
    └── Collection2
        ├── Document1
        └── Document2

---------------

> var c = db.col1.find()                                                                                            
> while(c.hasNext())printjson(c.next())                                                                             
{ "_id" : ObjectId("584396e96516646a4dfeab45"), "name" : "mongo" }
{
        "_id" : ObjectId("584397126516646a4dfeab46"),
        "string" : "hello",
        "array" : [
                "sun",
                "mon",
                "tue"
        ]
}
{ "_id" : ObjectId("584398486516646a4dfeab47"), "name" : "hogehoge" }
{ "_id" : ObjectId("584399996516646a4dfeab48"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("584399996516646a4dfeab49"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("584399996516646a4dfeab4a"), "x" : 4, "j" : 3 }

クライアントで接続

$ ./bin/mongo
> 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

        show dbs                     show database names
        show collections             show collections in current database
        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 <db_name>                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

コマンド

clientで接続

mongo [serverアドレス]/[DB名]

$ ./bin/mongo localhost/testdb                                     
MongoDB shell version: 3.2.11
connecting to: localhost/testdb

DB操作

DB切り替え・作成
$ use [DB名]

DB削除
$ use [DB名]
db.dropDatabase()
{ "ok" : 1 }

DB一覧
$ show dbs

Collection操作

MySQLでいうところのテーブルか。

一覧
use [DB名]
show collections

コレクション作成
use [DB名]
db.createCollection("col1")                                                                                       
{ "ok" : 1 }

コレクション削除
use [DB名]
db.col1.drop()

TAB補完

便利。

db.drop                                                                                                           
db.dropAllRoles(  db.dropAllUsers(  db.dropDatabase(  db.dropRole(      db.dropUser(

"not master and slaveOk=false"が出たとき

db.getMongo().setSlaveOk() 

参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?