サービス登録ってどうやるの?.netだと使い勝手悪いんじゃ?など
色々と調べる事があるから使わないでMSSQLやテキストファイルの
CSV,JSONで十分じゃんって自分に言いきかせて触らずにいたのだが
こういったデータは複数のプログラムでリアルタイムに更新したかったりするので
今回本気に使ってみることにした。
まず覚えなくてはいけない事。
1.MongoDBをダウンロードすること。
2.DB、LOGフォルダを用意すること。
3.サービス登録しておくこと。
4.VisualStudioのNugetで「Official MongoDB C# Driver」をインストールすること。(Apache2.0ライセンス)
5.ライブラリの使い方、DBの接続方法、データの追加、削除、更新を覚えること。
1,2,3まではやる気さえあればできそうで
4,5になると開発環境を起動したりコードを書いたりしなきゃならないので遠ざかりたくなるけど、使いたいという思いでどうにか乗り切るしかなさそう。
なるべく手順をまとめると、
Step1. MongoDBのダウンロード、DB、LOGフォルダの用意、サービス登録を行う。
http://www.mongodb.org/
上記公式サイトからMongoDB本体をダウンロードしてきて任意の場所に展開する。
そしてダウンロードしながらどこかにDB、LOGフォルダを用意してから
管理者権限でコマンドプロンプトを開く。
今回は
d:\tool\mongodb MongoDB展開フォルダ
d:\tool\mongodb\bin\log Logフォルダ
d:\tool\mongodb\bin\db DBフォルダ
というようにした場合、下記コマンドを入力する。
>cd \tool\mongodb\bin
>mongod --install --dbpath .\db --logpath .\log\log.txt
サービス解除する時は --install を --remove にすればOK。
Step2.使い方を覚える
まずは、VisualStudioを起動してソリューションを1つ作ってから
Nugetで「Official MongoDB C# Driver」をインストール。
あとはコーディング。
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders
Imports MongoDB.Driver.Linq
Imports MongoDB.Bson
Imports MongoDB.Bson.Serialization.Attributes
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
'MongoDBサーバーに接続して、操作するコレクションを選択。
Dim mongo_drv = New MongoClient()
Dim db = mongo_drv.GetServer().GetDatabase("TestDB")
Dim col = db.GetCollection(Of AddressModel)("AddressCollection")
'サンプルデータの追加
col.Insert(New AddressModel() With {
.Name = "test1",
.Birthday = Date.Parse("1980/12/5"),
.EMailAddress = New List(Of EMailModel)() From {
New EMailModel With {.DisplayName = "test1", .MailAddress = "mail1@test.com"},
New EMailModel With {.DisplayName = "test2", .MailAddress = "mail2@test.com"}
}
})
col.Insert(New AddressModel() With {
.Name = "test1",
.Birthday = Date.Parse("1981/12/5"),
.EMailAddress = New List(Of EMailModel)() From {
New EMailModel With {.DisplayName = "test3", .MailAddress = "mail3@test.com"},
New EMailModel With {.DisplayName = "test4", .MailAddress = "mail4@test.com"}
}
})
'MondoDBサーバー切断
mongo_drv.GetServer().Disconnect()
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
'MongoDBサーバーに接続して、操作するコレクションを選択。
Dim mongo_drv = New MongoClient()
Dim db = mongo_drv.GetServer().GetDatabase("TestDB")
Dim col = db.GetCollection(Of AddressModel)("AddressCollection")
'LINQで検索
For Each model In (From t In col.AsQueryable() Where t.Name.StartsWith("test") Select t)
Console.WriteLine("--------")
Console.WriteLine(model.Name)
Console.WriteLine(model.Birthday)
For Each emodel In model.EMailAddress
Console.WriteLine(emodel.DisplayName)
Console.WriteLine(emodel.MailAddress)
Next
Console.WriteLine("--------")
Next
'MondoDBサーバー切断
mongo_drv.GetServer().Disconnect()
End Sub
Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
'MongoDBサーバーに接続して、操作するコレクションを選択。
Dim mongo_drv = New MongoClient()
Dim db = mongo_drv.GetServer().GetDatabase("TestDB")
Dim col = db.GetCollection(Of AddressModel)("AddressCollection")
'LINQで検索、該当項目の日付を更新する(GMTがまちがってそう。。)
For Each model In (From t In col.AsQueryable() Where t.Name.StartsWith("test") Select t)
model.Birthday = Date.Now
col.Save(model)
Next
mongo_drv.GetServer().Disconnect()
End Sub
Private Sub Button_Click_3(sender As Object, e As RoutedEventArgs)
'MongoDBサーバーに接続して、操作するコレクションを選択。
Dim mongo_drv = New MongoClient()
Dim db = mongo_drv.GetServer().GetDatabase("TestDB")
Dim col = db.GetCollection(Of AddressModel)("AddressCollection")
'LINQで検索、該当項目の削除
For Each model In (From t In col.AsQueryable() Where t.Name.StartsWith("test") Select t)
col.Remove(Query.EQ("_id", model._id))
Next
mongo_drv.GetServer().Disconnect()
End Sub
End Class
Public Class AddressModel
Public Property _id As BsonObjectId
Public Property Name As String
Public Property Birthday As Date
Public Property EMailAddress As List(Of EMailModel)
End Class
Public Class EMailModel
Public Property DisplayName As String
Public Property MailAddress As String
End Class
日付の扱いがちょっとGMTがらみの調整が必要かもしれないけど、ひとまず使えるようになったのでここまで。
0 件のコメント:
コメントを投稿