2012年3月22日木曜日

MongoDB(2) はろーわーるど

つづき。MongoDBをつかってみます。

mongoシェルログイン
> mongo
MongoDB shell version: 2.0.3
connecting to: test
>

testデータベース(?)にログインしたっぽい。

create tableどーやるんだ。
ヘルプで確認。
> help
  db.help()                    help on db methods
  db.mycoll.help()             help on collection methods
  rs.help()                    help on replica set methods
  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 ....
  show logs                    show the accessible logger names
  show log [name]              prints out the last segment of log in ....
   use                 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....
  exit                         quit the mongo shell

create table ない。

もういっこのヘルプ参照
>db.mycoll.help()
DBCollection help
  db.mycoll.find().help() - show DBCursor help
  db.mycoll.count()
  db.mycoll.dataSize()
  db.mycoll.distinct( key ) - eg. db.mycoll.distinct( 'x' )
  db.mycoll.drop() drop the collection
  db.mycoll.dropIndex(name)
  db.mycoll.dropIndexes()
  db.mycoll.ensureIndex(keypattern[,options]) - options is an object withthese ....
  db.mycoll.reIndex()
  db.mycoll.find([query],[fields]) - query is an optional query filter. fields is op....
  db.mycoll.find(...).count()
  db.mycoll.find(...).limit(n)
  db.mycoll.find(...).skip(n)
  db.mycoll.find(...).sort(...)
  db.mycoll.findOne([query])
  db.mycoll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
  db.mycoll.getDB() get DB object associated with collection
  db.mycoll.getIndexes()
  db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] })
  db.mycoll.mapReduce( mapFunction , reduceFunction ,  )
  db.mycoll.remove(query)
  db.mycoll.renameCollection( newName ,  ) renames the collection.
  db.mycoll.runCommand( name ,  ) runs a db command with the gi....
  db.mycoll.save(obj)
  db.mycoll.stats()
  db.mycoll.storageSize() - includes free space allocated to this collection
  db.mycoll.totalIndexSize() - size in bytes of all the indexes
  db.mycoll.totalSize() - storage allocated for all data and indexes
  db.mycoll.update(query, object[, upsert_bool, multi_bool])
  db.mycoll.validate(  ) - SLOW
  db.mycoll.getShardVersion() - only for use with sharding
  db.mycoll.getShardDistribution() - prints statistics about data distribution in the cluster

みつからず。

mycollってのはコレクションのことっぽい。

ていうか、もしかして、
>db.example.save({name:'myname'})
>db.example.find();
{ "_id" : ObjectId("4f69656db3855bee2fdca9f2"), "name" : "myname" }

スキーマレスってそういうことかー。 すごい。
>db.example.save({age: 50})
>db.example.find()
{ "_id" : ObjectId("4f69656db3855bee2fdca9f2"), "name" : "myname" }
{ "_id" : ObjectId("4f696610b3855bee2fdca9f3"), "age" : 50 }

カオス!
> db.example.find({age: 50})
{ "_id" : ObjectId("4f696610b3855bee2fdca9f3"), "age" : 50 }

おもしろい!


まとめ

MongoDBはFacebookのデータ構造に近い。

かねこ(*´σ`)