MongoDBチュートリアル

Getting Started — MongoDB Manual

  • へぇ、create database とかやらないんだ。

Tip for Developers with Experience in Other Databases
You may notice, in the examples below,that we never create a database or collection.
MongoDB does not require that you do so. As soon as you insert something, MongoDB creates the underlying collection and database.
If you query a collection that does not exist, Mongo treats it as an empty collection.

> db.foo.save( { a : 1 } )
Sat Mar  6 07:36:11 allocating new datafile /data/db/test.ns, filling with zeroes...
Sat Mar  6 07:36:11 done allocating datafile /data/db/test.ns, size: 16MB, took 0.077 secs
Sat Mar  6 07:36:11 allocating new datafile /data/db/test.0, filling with zeroes...
Sat Mar  6 07:36:11 done allocating datafile /data/db/test.0, size: 64MB, took 0.321 secs
Sat Mar  6 07:36:12 building new index on { _id: ObjId(000000000000000000000000) } for test.foo...
Sat Mar  6 07:36:12 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: ObjId(000000000000000000000000) } }
Sat Mar  6 07:36:12 done for 0 records 0.167secs
Sat Mar  6 07:36:11 insert test.foo 907ms
> db.foo.find()
{ "_id" : ObjectId("4b9205eb3efb5959545ef16c"), "a" : 1 }
  • ID は勝手につくらしい。オブジェクトIDが。

Upon being inserted into the database,
objects are assigned an object ID (if they do not already have one) in the field _id.

  • シェルでの表示にはリミットがついているらしい(バージョンによってリミット値は違うっぽい)。そんなときは「it」コマンドを打つと続きを表示する。
  • あと、同じデータを入れてもいいのね。オブジェクトIDが違うから別モノ扱いか。
> for ( var i = 1; i < 10; i++) db.things.save( { x : 3, j : i } );
> db.things.find();
{ "_id" : ObjectId("4b9208e23f0e525cbcf43d7c"), "name" : "mongo" }
{ "_id" : ObjectId("4b9208f63f0e525cbcf43d7d"), "x" : 3 }
{ "_id" : ObjectId("4b9209c33f0e525cbcf43d7e"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4b9209c33f0e525cbcf43d7f"), "x" : 4, "j" : 2 }{ "_id" : ObjectId("4b9209c33f0e525cbcf43d86"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4b920b093f0e525cbcf43d87"), "x" : 3, "j" : 1 }
{ "_id" : ObjectId("4b920b093f0e525cbcf43d88"), "x" : 3, "j" : 2 }{ "_id" : ObjectId("4b920b093f0e525cbcf43d8f"), "x" : 3, "j" : 9 }
has more
> it
{ "_id" : ObjectId("4b920b1c3f0e525cbcf43d90"), "x" : 3, "j" : 1 }
{ "_id" : ObjectId("4b920b1c3f0e525cbcf43d91"), "x" : 3, "j" : 2 }{ "_id" : ObjectId("4b920b1c3f0e525cbcf43d98"), "x" : 3, "j" : 9 }
>
  • forEach で都度つど関数を宣言するやて?そない殺生な。。
> db.things.find().forEach( function(x) { print(tojson(x));});

In the case of a forEach() we must define a function that is called for each document in the cursor.

  • 関数を'{}' で囲むと怒られる。'()' で囲むというルール。なんでだろ。
> db.things.find().forEach{function(x){print(tojson(x));}};
Sat Mar  6 08:15:00 JS Error: SyntaxError: missing ; before statement (shell):0
  • スナップショット機能(データを丸ごとコピーしといて、いじってから元に戻す?) はないので、データいじるときはロックかけてから。

MongoDB cursors are not snapshots - operations performed by you or other users on the collection being queried between the first and last call to next() of your cursor may or may not be returned by the cursor. Use explicit locking to perform a snapshotted query.

  • findで条件をカンマで繋ぐとANDになるらしい。
> db.things.find({x:3, j:9}).forEach(function(x) { print(tojson(x));});
{ "_id" : ObjectId("4b920b093f0e525cbcf43d8f"), "x" : 3, "j" : 9 }

The query expression is an document itself. A query document of the form { a:A, b:B, ... } means "where a==A and b==B and ...".

  • 帰ってくる値を指定してもIDは必ず返ってくるのね。。しつこい人。

To illustrate, lets repeat the last example find({x:4}) with an additional argument that limits the returned document to just the "j" elements:

> db.things.find({x:3, j:9}, {j:true}).forEach(function(x) { print(tojson(x));});
{ "_id" : ObjectId("4b920b093f0e525cbcf43d8f"), "j" : 9 }
{ "_id" : ObjectId("4b920b1c3f0e525cbcf43d98"), "j" : 9 }

Note that the "_id" field is always returned.

  • findOne すると、オブジェクトIDが若いものが返ってくるみたい。
> db.things.find({x:3, j:9}, {j:true}).forEach(function(x) { print(tojson(x));});
{ "_id" : ObjectId("4b920b093f0e525cbcf43d8f"), "j" : 9 }
{ "_id" : ObjectId("4b920b1c3f0e525cbcf43d98"), "j" : 9 }
>
> print(tojson(db.things.findOne({x:3, j:9}, {j:true})));
{ "_id" : ObjectId("4b920b093f0e525cbcf43d8f"), "j" : 9 }