zoukankan      html  css  js  c++  java
  • asp.net(C#) 中 怎么使用 MongoDb

    1. 先引用以下Dll(如果找不到 到gethub上下载源代码自己编译 特别是MongoDB.Driver.Legacy.dll 我自己找了半天没找到):

    MongoDB.Bson.dll

    MongoDB.Driver.Core.dll

    MongoDB.Driver.dll

    MongoDB.Driver.Legacy.dll

    2. 以下是测试代码:

    using System;
    using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Driver.Linq;
    using MongoDB.Driver.Builders;
    
    namespace MongoDbDemo
    {
        public class MongoDbHelper
        {
            public static void TestMongoDb()
            {
                var connectionString = "mongodb://192.168.201.140:27017";
                var client = new MongoClient(connectionString);
                var server = client.GetServer();
                var database = server.GetDatabase("test");
                var collection = database.GetCollection<Entity>("entities");
    
                var entity = new Entity { Name = "Tom" };
                collection.Insert(entity);
                var id = entity.Id;
    
                var query = Query<Entity>.EQ(e => e.Id, id);
                entity = collection.FindOne(query);
    
                entity.Name = "Dick";
                collection.Save(entity);
    
                var update = Update<Entity>.Set(e => e.Name, "Harry");
                collection.Update(query, update);
    
                collection.Remove(query);
            }
        }
    
        public class Entity
        {
            public ObjectId Id { get; set; }
    
            public string Name { get; set; }
        }
    }
  • 相关阅读:
    微软软件
    绘图软件安装出错解决方法
    Windows平台 Faster-RCNN 制作自己的数据集
    POJ2456 Agressive Cows
    P1030 求先序排列
    Luogu P2015二叉苹果树
    P2234 [HNOI2002]营业额统计
    Luogu P1347排序
    Luogu P1038神经网络
    Luogu P1006传纸条
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/4390138.html
Copyright © 2011-2022 走看看