MongoDB学习(四)——MongoDB修改记录

    链接分享:

刚刚实现了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"); 
            }

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值