MongoDB簡易コマンド(Win7 64bit)

MongoDB基本コマンド(超簡易)

ダウンロード(Win7 64bit)
MongoDB Download Center | MongoDB

配置とヘルプまで

D:\mongo\bin>mongo.exe --help
MongoDB shell version: 2.2.2
usage: mongo.exe [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no 'db address'
arg expected
--norc will not run the ".mongorc.js" file on start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)

file names: a list of files to run. files have to end in .js and will exit after
unless --shell is specified

データ格納用ディレクトリの生成

D:\mongo\bin>mkdir \data
D:\mongo\bin>mkdir \data\db

MongoDB起動

D:\mongo\bin>mongod.exe
mongod.exe --help for help and startup options
Wed Dec 05 11:00:22 [initandlisten] MongoDB starting : pid=5400 port=27017 dbpat
h=\data\db\ 64-bit host=tako-pc
Wed Dec 05 11:00:22 [initandlisten] db version v2.2.2, pdfile version 4.5
Wed Dec 05 11:00:22 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5a
f9d664267
Wed Dec 05 11:00:22 [initandlisten] build info: windows sys.getwindowsversion(ma
jor=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB
_VERSION=1_49
Wed Dec 05 11:00:22 [initandlisten] options: {}
Wed Dec 05 11:00:22 [initandlisten] journal dir=/data/db/journal
Wed Dec 05 11:00:22 [initandlisten] recover : no journal files present, no recov
ery needed
Wed Dec 05 11:00:22 [websvr] admin web console waiting for connections on port 2
8017
Wed Dec 05 11:00:22 [initandlisten] waiting for connections on port 27017
Wed Dec 05 11:05:43 [initandlisten] connection accepted from 127.0.0.1:51554 #1
(1 connection now open)

別窓でMongoDBクライアント起動

D:\>cd mongo\bin
D:\mongo\bin>mongo.exe
MongoDB shell version: 2.2.2
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user

コマンド打ちして各種動作確認

エントリーを見る

> show dbs;
local (empty)

利用DB指定(エントリーに存在しないのでも使えるみたい)

> use tako;
switched to db tako;

コレクションの作成(上でUSE指定のエントリー内に作られる)

> db.createCollection("hati");
{ "ok" : 1 }
> show collections;
hati
system.indexes
> db.createCollection("hati2");
{ "ok" : 1 }
> show collections;
hati
hati2
system.indexes

データエントリー(レコードを格納)

> db.hati.save({takoid:'1',takovalue:'aiueo',takono:'001'});

>

データ抽出

> db.hati.find();
{ "_id" : ObjectId("50beb681611fd0fc8d54830f"), "takoid" : "1", "takovalue" : "aiueo", "takono" : "001" }
{ "_id" : ObjectId("50beb691611fd0fc8d548310"), "takoid" : "2", "takovalue" : "kakikukeko", "takono" : "002" }
>

データ検索

> db.hati.find({"takono":"002"});
{ "_id" : ObjectId("50beb691611fd0fc8d548310"), "takoid" : "2", "takovalue" : "kakikukeko", "takono" : "002" }
>