zoukankan      html  css  js  c++  java
  • 5.使用驱动实现基本数据的操作

    常用的两种驱动下载

    通过samus驱动实现HelloWorld存取

    • 首先检查服务是否已经开启;
    • 引用MongoDB.dll 文件
    //链接字符串
    string connectionString = "mongodb://localhost";
    
    //数据库名
    string databaseName = "myDatabase";
    
    //集合名
    string collectionName = "myCollection";
    
    //定义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"] = 1;
        doc["Msg"] = "Hello World!";
    
        //将这个文档对象插入集合
        mongoCollection.Insert(doc);
    
        //在集合中查找键值对为ID=1的文档对象
        Document docFind = mongoCollection.FindOne(new Document { { "ID", 1 } });
    
        //输出查找到的文档对象中键“Msg”对应的值,并输出
        Console.WriteLine(Convert.ToString(docFind["Msg"]));
    }
    finally
    {
        //关闭链接
        mongo.Disconnect();
    }
    
    

    运行程序,成功打印了helloworld,我们打开数据文件夹 发现多了两个文件 “myDatabase.ns”和“myDatabase.0”

  • 相关阅读:
    ie兼容,手机端兼容问题
    JS-cookie和正则表达式
    第六节蓝桥杯 方格填数
    最长公共子串长度
    上台阶问题
    各种好东西
    Painting the balls (dp优化)
    Nobita's New Filesystem (bitset)
    恐狼后卫 (区间dp)
    CF701F String set queries (分块思想+暴力)
  • 原文地址:https://www.cnblogs.com/maanshancss/p/12937771.html
Copyright © 2011-2022 走看看