zoukankan      html  css  js  c++  java
  • Mongodb入门代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MongoDB;


    namespace TestMongodb
    {
        public partial class _Default : System.Web.UI.Page
        {

            //链接字符串
            string connectionString = "mongodb://localhost";
            //数据库名
            string databaseName = "test";
            //集合名
            string collectionName = "myCollection";


            protected void Page_Load(object sender, EventArgs e)
            {
                //insertData();
                //deleteData();
                updateData();
                QueryAll();
            }

            private void updateData()
            {
                //定义Mongo服务
                Mongo mongo = new Mongo(connectionString);
                //获取databaseName对应的数据库,不存在则自动创建
                MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
                //获取collectionName对应的集合,不存在则自动创建
                MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
                //链接数据库
                mongo.Connect();
                try
                {

                    Document docFind = mongoCollection.FindOne(new Document { { "ID", 3 } });
                    docFind["Msg"] = "ssso";
                    mongoCollection.Update(docFind);
                }
                finally
                {
                    //关闭链接
                    mongo.Disconnect();
                }
               
            }

            public void insertData()
            {
                //定义Mongo服务
                Mongo mongo = new Mongo(connectionString);
                //获取databaseName对应的数据库,不存在则自动创建
                MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
                //获取collectionName对应的集合,不存在则自动创建
                MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
                //链接数据库
                mongo.Connect();
                try
                {
                    //定义一个文档对象,存入一个键值对
                    Document doc = new Document();
                    doc["ID"] = 3;
                    doc["Msg"] = "helloword3!";
                    //将这个文档对象插入集合
                    mongoCollection.Insert(doc);
                    //在集合中查找键值对为ID=1的文档对象
                    Document docFind = mongoCollection.FindOne(new Document { { "ID", 1 } });
                    //输出查找到的文档对象中键“Msg”对应的值,并输出
                    Console.WriteLine(Convert.ToString(docFind["Msg"]));
                }
                finally
                {
                    //关闭链接
                    mongo.Disconnect();
                }

            }

            public void deleteData()
            {
                //定义Mongo服务
                Mongo mongo = new Mongo(connectionString);
                //获取databaseName对应的数据库,不存在则自动创建
                MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
                //获取collectionName对应的集合,不存在则自动创建
                MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
                //链接数据库
                mongo.Connect();
                try
                {

                    Document docFind = mongoCollection.FindOne(new Document { { "ID", 1 } });
                    mongoCollection.Delete(docFind);
                }
                finally
                {
                    //关闭链接
                    mongo.Disconnect();
                }

            }

            public void QueryAll()
            {
                //定义Mongo服务
                Mongo mongo = new Mongo(connectionString);
                //获取databaseName对应的数据库,不存在则自动创建
                MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
                //获取collectionName对应的集合,不存在则自动创建
                MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
                //链接数据库
                mongo.Connect();
                try
                {
                    IEnumerable<Document> documents = mongoCollection.FindAll().Documents;
                    foreach (Document d in documents)
                    {
                        Response.Write(d["ID"].ToString() + d["Msg"].ToString() +"<br/>");
                    }
                }
                finally
                {
                    //关闭链接
                    mongo.Disconnect();
                }

            }

           

        }
    }

  • 相关阅读:
    cocos2dx打飞机项目笔记七:各种回调:定时器schedule、普通回调callFunc、菜单回调menu_selector、事件回调event_selector
    cocos2dx打飞机项目笔记六:GameScene类和碰撞检测 boundingbox
    [Redis] 手动搭建标准6节点Redis集群(docker)
    [JavaSE 源码分析] 关于HashMap的个人理解
    [leetcode 周赛 150] 1161 最大层内元素和
    [leetcode 周赛 150] 1160 拼写单词
    [leetcode 周赛 149] 1157 子数组中占绝大多数的元素
    [leetcode 周赛 149] 1156 单字符重复子串的最大长度
    [leetcode 周赛 149] 1155 掷骰子的N种方法
    [leetcode 周赛 149] 1154 一年中的第几天
  • 原文地址:https://www.cnblogs.com/kenny999/p/2323047.html
Copyright © 2011-2022 走看看