tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

mongoシェルでServer-Side Functionsをsave&loadして使ってみる

Server-Side Functionsはいろいろ使い道もあるかと思いますが
今回は「サンプルデータを好きなときに何回もつっこんでみる」っていう例を元に使った感じをメモしておきます

もちろん他の方法のほうが良かったりもするんですが今回はあえてこれでやるということで。。w

こんな時例

・サンプルデータを適当につっこみたい
・適当につっこんだサンプルデータをupdateとかしたい
・そして何度もやり直したい(サンプルデータの初期化したい。mongoシェルから出ずに)

Server-Side Functionsで「こんな時例」を実現してみる

使い方

使い方は簡単で
・定義する(最初だけ)
・ロードする
・使う
な感じで使います

※定義する
> db.system.js.save({ "_id" : "testFunc", "value" : function(a){print(a)} })

※ロードする
> db.loadServerScripts()

※使う
> testFunc("ohayou")
ohayou

参考サイトも載せておきます
https://github.com/syokenz/marunouchi-mongodb/tree/master/20120926/syokenz

てことでサンプルデータをdropしてinsertするのをServer-Side Functionsにしてみる
※定義する
> db.system.js.save({"_id": "initSample", "value":function(){
...   db.test.drop();
...   db.test.insert({"_id": "a00001", "user_info" : {"name": "hoge", "age": 10}, "user_prof" : {"work": 0, "smorker": 0 } });
...   db.test.insert({"_id": "a00002", "user_info" : {"name": "fuga", "age": 21}, "user_prof" : {"work": 1, "smorker": 0 } });
...   db.test.insert({"_id": "a00003", "user_info" : {"name": "piyo", "age": 33}, "user_prof" : {"work": 1, "smorker": 1 } });
...   db.test.insert({"_id": "a00004", "user_info" : {"name": "waru", "age": 40}, "user_prof" : {"work": 0, "smorker": 1 } });
... }})

※使ってみる→怒られる
>initSample()
Wed Mar 19 22:26:39.270 ReferenceError: initSample is not defined

※ロードする
> db.loadServerScripts()

※Server-Side Functions使う
>initSample()

※内容見てみる
>db.test.find()
{ "_id" : "a00001", "user_info" : { "name" : "hoge", "age" : 10 }, "user_prof" : { "work" : 0, "smorker" : 0 } }
{ "_id" : "a00002", "user_info" : { "name" : "fuga", "age" : 21 }, "user_prof" : { "work" : 1, "smorker" : 0 } }
{ "_id" : "a00003", "user_info" : { "name" : "piyo", "age" : 33 }, "user_prof" : { "work" : 1, "smorker" : 1 } }
{ "_id" : "a00004", "user_info" : { "name" : "waru", "age" : 40 }, "user_prof" : { "work" : 0, "smorker" : 1 } }

※updateしてみる
>db.test.update({"_id" : "a00001"}, {"$set": {"name" : "aaaaaaaaaaaaaaaaaaaaaaaa"}})

※内容が変わってる
>db.test.find()
{ "_id" : "a00002", "user_info" : { "name" : "fuga", "age" : 21 }, "user_prof" : { "work" : 1, "smorker" : 0 } }
{ "_id" : "a00003", "user_info" : { "name" : "piyo", "age" : 33 }, "user_prof" : { "work" : 1, "smorker" : 1 } }
{ "_id" : "a00004", "user_info" : { "name" : "waru", "age" : 40 }, "user_prof" : { "work" : 0, "smorker" : 1 } }
{ "_id" : "a00001", "name" : "aaaaaaaaaaaaaaaaaaaaaaaa", "user_info" : { "name" : "hoge", "age" : 10 }, "user_prof" : { "work" : 0, "smorker" : 0 } }

※もっかいServer-Side Functions使ってみる
>initSample()

※初期化されてる
>db.test.find()
{ "_id" : "a00001", "user_info" : { "name" : "hoge", "age" : 10 }, "user_prof" : { "work" : 0, "smorker" : 0 } }
{ "_id" : "a00002", "user_info" : { "name" : "fuga", "age" : 21 }, "user_prof" : { "work" : 1, "smorker" : 0 } }
{ "_id" : "a00003", "user_info" : { "name" : "piyo", "age" : 33 }, "user_prof" : { "work" : 1, "smorker" : 1 } }
{ "_id" : "a00004", "user_info" : { "name" : "waru", "age" : 40 }, "user_prof" : { "work" : 0, "smorker" : 1 } }

saveされてるServer-Side Functionsを見る

ちなみに、saveされているServer-Side Functionsは普通にfindでみれます

> db.system.js.find()
{ "_id" : "initSample", "value" : function (){
  db.test.drop();
  db.test.insert({"_id": "a00001", "user_info" : {"name": "hoge", "age": 10}, "user_prof" : {"work": 0, "smorker": 0 } });
  db.test.insert({"_id": "a00002", "user_info" : {"name": "fuga", "age": 21}, "user_prof" : {"work": 1, "smorker": 0 } });
  db.test.insert({"_id": "a00003", "user_info" : {"name": "piyo", "age": 33}, "user_prof" : {"work": 1, "smorker": 1 } });
  db.test.insert({"_id": "a00004", "user_info" : {"name": "waru", "age": 40}, "user_prof" : {"work": 0, "smorker": 1 } });
} }
{ "_id" : "testFunc", "value" : function (a){print(a)} }

いろいろと使えそうですね!