链接分享:
刚刚实现了MongoDB的增删改查,有其他的理解还有多多提出啊。
http://www.cnblogs.com/viprx/archive/2012/09/07/2674637.html
<http://www.tuicool.com/articles/JNNJjuf>
http://blog.csdn.net/work888study/article/details/9113027
http://blog.csdn.net/huwei2003/article/details/6060201
准备工具
VS2012,MongoDB.Bson,MongoDB.Driver DLL文件,Log4net的nuget安装,MongoDB服务器。
本地服务开启 net start MongoDB
创建前台html页面,触发AJAX方法调用后台 ashx的方法
//rename
$(document)(function(){
$("#reName").click(function () {
$.ajax({
type: "POST",
url: "MongoDBHelper.ashx",
data: { action: "RENAME", filename: "123.txt" },
success: function (data) {
alert(data);
getFiles();
},
error: function (err) {
alert(err);
}
});
});
}
代码实现
引用的程序集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Internal;
using System.Configuration;
using System.Reflection;
using log4net.Config;
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
namespace MongoDBTest
后台真正实现方法,由一般处理程序ProcessRequest调用真正实现方法
public void ProcessRequest(HttpContext context)
{
reName(context);
}
//modify name of file
public void reName(HttpContext context)
{
try
{
// get filename of Modified
string file = context.Request["filename"];
// steup 1: Create MongoDB Connection And No User Authentication
//shell Command ——mongo 127.0.0.1:27017
MongoServer server = MongoServer.Create("mongodb://127.0.0.1:27017");
//steup 2: Get Database shell Command——use DB3
MongoDatabase db = server.GetDatabase("DB3");
//steup 3: Get Collection Of Database shell Command—— db.fs.files.***
MongoCollection<String> col = db.GetCollection<String>("fs.files");
//steup 4: Create Query shell Command——db.fs.files.update({query},{$set})
var query = Query.And(Query.EQ("filename", file));
//setup 5: Create $Set Modifiers
var update = Update.Set("filename", "我的文件.txt");
//steup 6: Execute execute
col.Update(query, update);
}
catch (Exception e)
{
// Error Log Processing Of Log4net
log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log.Error("error", e);
log.Fatal("fatal", e);
log.Info("Info",e);
log.Debug("dubug");
log.Warn("warn");
}