public IActionResult Insert() { var client = new MongoClient(Website.Instance.Configuration["MangoDB"]);//連結db語法 var database = client.GetDatabase("testMangoDB"); testFile temp = new testFile(); temp.userName = "******"; temp.userPassword = "******"; temp.userAccount = "YoungChen"; temp.wishList = new List <wish>(); temp.wishList.Add(new wish { prodName = "test", prodOid = "1" }); temp.wishList.Add(new wish { prodName = "test2", prodOid = "2" }); var testFileCollection = database.GetCollection <testFile>("testFile"); //連接資料表 testFileCollection.InsertOneAsync(temp); //Insert Dictionary <string, string> jsonResult = new Dictionary <string, string>(); jsonResult.Add("result", "OK"); return(Json(jsonResult)); }
public IActionResult Update() { var client = new MongoClient(Website.Instance.Configuration["MangoDB"]); //連結db語法 var database = client.GetDatabase("testMangoDB"); var testFileCollection = database.GetCollection <testFile>("testFile"); //連接資料表 var filter = Builders <testFile> .Filter.Eq(x => x.userAccount, "YoungChen"); //查詢條件 testFile testFile = testFileCollection.Find(filter).FirstOrDefault(); testFile.userAccount = "YoungUpdate"; ReplaceOneResult result = testFileCollection.ReplaceOne(filter, testFile); Dictionary <string, string> jsonResult = new Dictionary <string, string>(); jsonResult.Add("result", "OK"); return(Json(jsonResult)); }