Azure の DocumentDB が Mongo プロトコルをサポートしているのを今更知りました。( 情弱.. ) 過去に自分の中で MongoDB ブームがあり、少しだけ触れてました。
ryuchan.hatenablog.com
ryuchan.hatenablog.com
DocumentDB についても記事書いていましたね。完全に記憶からなくなってました。
ryuchan.hatenablog.com
早速、試してみましょう。最近、Azure 自体あまり使ってませんでした。( 業務で使わないと、どうしても疎遠になりがち... ) Azure 上で Mongo 互換の DocumentDB を作成しました。
MongoDB のドライバはここです。NuGet しましょう。
- MongoDB.Driver: The new driver. It is mostly free of any legacy code and should be used for all new projects. More documentation can be found in the reference guide.
- MongoDB.Driver.Core: The core of the driver and a dependency of MongoDB.Driver. You will probably not use this package directly. More documentation can be found in the reference guide.
- MongoDB.Bson: The BSON layer. It is a dependency of MongoDB.Driver.Core. It may be used by itself. More documentation can be found in the reference guide.
- mongocsharpdriver: The compatibility layer for those upgrading from our 1.x series. This should not be used for new projects. More information can be found in the 1.x documentation;
はるか昔の記事で、下記のようなコードを書いていました。今回は下記のソースを MongoDB プロトコルをサポートしている Azure DocumentDB で実行させたいと思います。
//前回のソースそのまま。 var monc = new MongoClient(); //前回のソースそのまま。 var mons = monc.GetServer(); //データベースの取得 var db = mons.GetDatabase("Chart"); //Chartのドキュメントタイプを取得する。 var chart = db.GetCollection<BsonDocument>("Chart"); //BsonDocumentクラスを使用して、JSON形式で格納するデータ作成する。 var chartdocument = new BsonDocument { { "PatientID", "1" }, { "DocumentNo", "D0001" }, { "DocumentContents", "頭が痛い。"}}; //データをInsertする。 chart.Insert(chartdocument);
SQL Database などと同様に接続文字列を使用します。接続文字列の情報は下記の通りです。
接続文字列を使用するため、MongoClient クラスの使い方を変える必要があります。
MongoClient Constructor
接続文字列を直接渡したいので、下記のコンストラクタを使用してみましょう。
public MongoClient(
string connectionString
)
GetServer は非推奨になってるので、使用しないほうがいいですね。
MongoClientExtensions.GetServer Method
[ObsoleteAttribute("Use the new API instead.")]
public static MongoServer GetServer(
this MongoClient client
)
あと、IMongoCollection の insert メソッドはなくってますね。代替メソッドとして、下記のメソッドに追加されています。
IMongoCollection(TDocument).InsertOne Method
IMongoCollection(TDocument).InsertOneAsync Method (TDocument, CancellationToken)
IMongoCollection(TDocument).InsertMany Method
IMongoCollection(TDocument).InsertManyAsync Method
これらの結果を踏まえて、下記の通りにコードを作成しました。昨日インストールした Visual Studio で実行してみます。
//接続文字列を設定 var connectionString = "mongodb://...[略]...documents.azure.com:10250/?ssl=true"; //接続文字列を指定 var monc = new MongoClient(connectionString); //データベースの取得 var db = monc.GetDatabase("Chart"); //Chartのドキュメントタイプを取得する。 var chart = db.GetCollection<BsonDocument>("Chart"); //BsonDocumentクラスを使用して、JSON形式で格納するデータ作成する。 var chartdocument = new BsonDocument { { "PatientID", "1" }, { "DocumentNo", "D0001" }, { "DocumentContents", "頭が痛い。"} }; //データを一件追加する。 chart.InsertOne(chartdocument);
DocumentDB の見方がわからないので、よくわかりませんがコレクションが 0 → 1 に変化したので何か更新されたのでしょうwww
NoSQL は楽しいですね。しかし、DocumentDB に格納したデータはどうやって確認したらいいんでしょうか? ( Azure ポータル上からは参照できなさそうです。 ) 面白そうなので、ちょっと調べてみよう。
※可能でした。クエリエクスプローラーから参照できます。(from BEACHSIDE (id:beachside)さん)
beachside.hatenablog.com
さて、Ignite 見ましょうかね。
ryuchan.hatenablog.com
書籍いっぱいありますね。うらやましい。
- 作者: 渡部徹太郎,河村康爾,北沢匠,佐伯嘉康,佐藤直生,原沢滋,平山毅,李昌桓
- 出版社/メーカー: 秀和システム
- 発売日: 2016/02/24
- メディア: 単行本
- この商品を含むブログ (2件) を見る
- 作者: Kyle Banker,Sky株式会社玉川竜司
- 出版社/メーカー: オライリージャパン
- 発売日: 2012/12/14
- メディア: 大型本
- 購入: 5人 クリック: 55回
- この商品を含むブログ (8件) を見る
- 作者: 小笠原徳彦
- 出版社/メーカー: 工学社
- 発売日: 2016/07
- メディア: 単行本
- この商品を含むブログ (1件) を見る
- 作者: 本橋信也,河野達也,鶴見利章
- 出版社/メーカー: リックテレコム
- 発売日: 2014/03/06
- メディア: Kindle版
- この商品を含むブログを見る
シングルページWebアプリケーション ―Node.js、MongoDBを活用したJavaScript SPA
- 作者: Michael S. Mikowski,Josh C. Powell,佐藤直生,木下哲也
- 出版社/メーカー: オライリージャパン
- 発売日: 2014/05/24
- メディア: 大型本
- この商品を含むブログ (2件) を見る