MongoDB增删改查

WebForm1.aspx前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WR.Web.NewTechnology.MongoDB.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnInsert" runat="server" Text="Insert" 
            onclick="btnInsert_Click" />
        <asp:Button ID="btnRemove"
            runat="server" Text="Remove" onclick="btnRemove_Click" />
        <asp:Button ID="btnUpdate" runat="server" Text="Update" 
            onclick="btnUpdate_Click" />
        <asp:Button ID="btnFindAll" runat="server" Text="Select" 
            onclick="btnFindAll_Click" />
    </div>
    </form>
</body>
</html>


WebForm1.aspx.cs后台代码

/// <summary>
    /// 作者:
    /// 日期:2012-9-9
    /// </summary>
    public partial class WebForm1 : System.Web.UI.Page
    {
        //链接字符串
        public static string connectionString = "mongodb://localhost";
        //数据库名
        public static string databaseName = "myDatabase";
        //集合名(相当于表)
        public static string collectionName = "myCollection";
        //定义Mongo服务
        static Mongo mongo = new Mongo(connectionString);
        //获取databaseName对应的数据库,不存在则自动创建
        static MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
        //获取collectionName对应的集合,不存在则自动创建
        MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Insert事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MongoInsert();
        }

        /// <summary>
        /// Remove事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            MongoRemove();
        }

        /// <summary>
        /// Update事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            MongoUpdate();
        }

        /// <summary>
        /// MongoSelect事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFindAll_Click(object sender, EventArgs e)
        {
            MongoSelect();
        }
        /// <summary>
        /// MongoDB操作Document对象——》添加:Insert
        /// </summary>
        public void MongoInsert()
        {
            //链接数据库
            mongo.Connect();
            try
            {
                //定义一个文档对象,存入一个键值对
                Document doc = new Document();
                doc["ID"] = 1;
                doc["Msg"] = "helloword!";

                Document doc2 = new Document();
                doc2["ID"] = 2;
                doc2["Msg"] = "helloword2!";

                Document doc3 = new Document();
                doc3["ID"] = 3;
                doc3["Msg"] = "helloword3!";

                //将这个文档对象插入集合
                mongoCollection.Insert(doc);
                mongoCollection.Insert(doc2);
                mongoCollection.Insert(doc3);

                //在集合中查找键值对为ID=1的文档对象
                Document docFind1 = mongoCollection.FindOne(new Document { { "ID", 1 } });
                Document docFind2 = mongoCollection.FindOne(new Document { { "ID", 2 } });
                Document docFind3 = mongoCollection.FindOne(new Document { { "ID", 3 } });

                //输出查找到的文档对象中键“Msg”对应的值,并输出
                MessageBox.Show(Page, Convert.ToString(docFind1["Msg"]) + "**" + Convert.ToString(docFind2["Msg"]) + "**" + Convert.ToString(docFind3["Msg"]));
            }
            finally
            {
                //关闭链接
                mongo.Disconnect();
            }
        }

        /// <summary>
        /// MongoDB操作Document对象——》移除:Remove
        /// </summary>
        public void MongoRemove()
        {
            //链接数据库
            mongo.Connect();
            try
            {
                mongoCollection.Remove(new Document { { "ID", 1 } });
                mongoCollection.Remove(new Document { { "ID", 2 } });
                mongoCollection.Remove(new Document { { "ID", 3 } });
                MessageBox.Show(Page, "恭喜您,删除成功!");
            }
            finally
            {
                //关闭链接
                mongo.Disconnect();
            }
        }

        /// <summary>
        /// MongoDB操作Document对象——》修改:Update
        /// </summary>
        public void MongoUpdate()
        {
            //链接数据库
            mongo.Connect();
            try
            {
                //定义一个文档对象,存入一个键值对
                Document doc = new Document();
                doc["ID"] = 1;
                doc["Msg"] = "UpdateHelloword!";

                Document doc2 = new Document();
                doc2["ID"] = 2;
                doc2["Msg"] = "UpdateHelloword2!";

                Document doc3 = new Document();
                doc3["ID"] = 3;
                doc3["Msg"] = "UpdateHelloword3!";

                //将这个文档对象插入集合
                mongoCollection.FindAndModify(doc, new Document { { "ID", 1 } });
                mongoCollection.FindAndModify(doc2, new Document { { "ID", 2 } });
                mongoCollection.FindAndModify(doc3, new Document { { "ID", 3 } });

                //在集合中查找键值对为ID=1的文档对象
                Document docFind1 = mongoCollection.FindOne(new Document { { "ID", 1 } });
                Document docFind2 = mongoCollection.FindOne(new Document { { "ID", 2 } });
                Document docFind3 = mongoCollection.FindOne(new Document { { "ID", 3 } });

                //输出查找到的文档对象中键“Msg”对应的值,并输出
                MessageBox.Show(Page, Convert.ToString(docFind1["Msg"]) + "**" + Convert.ToString(docFind2["Msg"]) + "**" + Convert.ToString(docFind3["Msg"]));
            }
            finally
            {
                //关闭链接
                mongo.Disconnect();
            }
        }

        /// <summary>
        /// MongoDB操作Document对象——》查询:FindAll
        /// </summary>
        public void MongoSelect()
        {
            string str = "";
            //链接数据库
            mongo.Connect();
            try
            {
                foreach (Document doc in FindAll())
                {
                    str += "ID:" + Convert.ToString(doc["ID"]) + "," + "Msg:" + Convert.ToString(doc["Msg"]) + ";";
                }
                //输出查找到的文档对象中键“Msg”对应的值,并输出
                MessageBox.Show(Page, str);
            }
            finally
            {
                //关闭链接
                mongo.Disconnect();
            }
        }

        /// <summary>
        /// 查找所有用户记录
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Document> FindAll()
        {
            return mongoCollection.FindAll().Documents;
        }
    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值