C#與mongoDB初始環境搭建

mongoDB官網https://www.mongodb.com/mongodb

mongoDB默認安裝路徑(Windows x64平臺) C:\Program Files\MongoDB\Server\3.4\bin數據庫

mongoDB環境變量配置 <img src="https://images2018.cnblogs.com/blog/741758/201806/741758-20180627181321580-384591787.png" width="600px" />post

啓動mongoDB服務ui

> mongod -dbpath D:\mongodb                
> mongo

<img src="https://images2018.cnblogs.com/blog/741758/201806/741758-20180627192241625-653322058.png" width="600px" />url

Visual Studio引用mongoDB.net

PM> install-package MongoDB.Driver

<img src="https://images2018.cnblogs.com/blog/741758/201806/741758-20180627195829644-1937979504.png" width="600px" />日誌

C#操做詳見 https://docs.mongodb.com/ecosystem/drivers/csharp/code

string url = "mongodb://127.0.0.1:27017";
 var client = new MongoClient(url);
 var db = client.GetDatabase("blog");//獲取數據庫,若是不存在就建立一個
 var collection = db.GetCollection<Post>("post");
 //增
 //for(int i = 1; i < 10000; i++) {
 //    Post post = new Post()
 //    {
 //        Id = i,
 //        Author = "做者" + i,
 //        CreateTime = DateTime.Now,
 //        Issue = false,
 //        Title = "文章" + i,
 //        Details = Guid.NewGuid().ToString(),
 //        Comments = new List<string>() { "評論" + i }
 //    };
 //    collection.InsertOne(post);
 //}

 //查
 //   Console.WriteLine(collection.CountDocuments(c => 1 == 1));
 //  var p5 = collection.Find(p => p.Id == 5).First();
 // Console.WriteLine("{0}在{1}發表了{2}", p5.Author, p5.CreateTime, p5.Title);

 //刪
 // collection.FindOneAndDelete<Post>(p => p.Id == 6);
 //var p6 = collection.Find<Post>(p => p.Id == 6).FirstOrDefault();

 //改
 // var filter = Builders<Post>.Filter.Eq("Id", 7);
 // var update = Builders<Post>.Update.Set("Title", "夏天");
 //collection.FindOneAndUpdate<Post>(filter, update);
 //var p7 = collection.Find(f => f.Id == 7).FirstOrDefault();
 // Console.WriteLine("{0}在{1}發表了{2}", p7.Author, p7.CreateTime, p7.Title);

配置mongoDB服務 在mongoDB的安裝目錄下建立配置文件mongod.cfg,而後再配置文件中添加服務的日誌的輸出路徑(對路徑沒有強制要求)blog

systemLog:
    destination: file
    path: D:\mongodb\logs\mongodb.log
storage:
    dbPath: D:\mongodb\data

經過管理員打開cmd.exeget

> mongod --config "D:\mongoDB\mongod.cfg" --install --serviceName "MongoDB"

<img src="https://images2018.cnblogs.com/blog/741758/201806/741758-20180627223926872-887033864.png" width="600px" />

相關文章
相關標籤/搜索