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; }
        }
    }
  • 相关阅读:
    451. Sort Characters By Frequency
    424. Longest Repeating Character Replacement
    68. Text Justification
    44. Wildcard Matching
    160. Intersection of Two Linked Lists
    24. Swap Nodes in Pairs
    93. 递归实现组合型枚举
    98. 分形之城
    97. 约数之和
    96. 奇怪的汉诺塔
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/4390138.html
Copyright © 2011-2022 走看看