xai1981's blog

http://twitter.com/xai1981

MongoDB 使ってみた

すでにインストール済みの環境でしたので、インストールはまた後日にレポートします。

起動
[user@mgo.dev ~]$ mongo
MongoDB shell version: 1.6.4
connecting to: test
終了
> exit
bye
データベースの作成
> use test_database
switched to db test_database
データベースの一覧を取得
> show dbs
test_database
データベースの切り替え
> use test_database2
switched to db test_database2
コレクションの作成
> db.createCollection("test_collection")
{ "ok" : 1 }
データーベース内のコレクション一覧を取得
> db.getCollectionNames()
[ "test_collection", "system.indexes" ]
コレクションにドキュメントを挿入
> db.test_collection.insert({username:"hoge", count: 100})
コレクションからドキュメントを取得
> db.test_collection.find()
{ "_id" : ObjectId("52a6ddd3ba04572dcc628025"), "count" : 100, "username" : "hoge" }
コレクションから特定のドキュメントを更新
> db.test_collection.update({username:"hoge"},{$set:{username:"bar"}})
> db.test_collection.find()
{ "_id" : ObjectId("52a6ddd3ba04572dcc628025"), "count" : 100, "username" : "bar" }
コレクションから特定のドキュメントを削除
> db.test_collection.remove({username:"bar"})
> db.test_collection.find()
参考サイト