首页 > 数据库 >netcore MongoDB

netcore MongoDB

时间:2022-08-21 09:11:09浏览次数:69  
标签:set get MongoDB netcore Value Id public

NuGet MongoDB.Driver

using MongoDB.Bson;
using MongoDB.Driver;
using System;

namespace MongoTest
{
    public class MongoHp
    {
        static MongoHp()
        {
            MongoDB.Bson.Serialization.BsonSerializer.RegisterSerializer(typeof(DateTime), new MongoDB.Bson.Serialization.Serializers.DateTimeSerializer(DateTimeKind.Local));
        }

        public static Tuple<MongoClient, IMongoDatabase, IMongoCollection<BaseCollect<T>>> GetCollection<T>() where T : class
        {
            MongoUrlBuilder bd = new MongoUrlBuilder();
            bd.Server = new MongoServerAddress("localhost", 27017);
            bd.MinConnectionPoolSize = 9;
            bd.MaxConnectionPoolSize = 99;

            var url = bd.ToMongoUrl();
            var client = new MongoClient(url);
            var database = client.GetDatabase("wms");
            var collection = database.GetCollection<BaseCollect<T>>(typeof(T).Name);
            return Tuple.Create(client, database, collection);
        }
    }

    public class BaseCollect<T> where T : class
    {
        public BaseCollect(T t)
        {
            this.Value = t;
        }

        public ObjectId Id { get; set; }

        public T Value { get; set; }

        public void InsertDb()
        {
            MongoHp.GetCollection<T>().Item3.InsertOne(this);
        }

        public UpdateResult UpdateDb(IMongoCollection<BaseCollect<T>> collection = null)
        {
            if (collection == null)
            {
                return MongoHp.GetCollection<T>().Item3.UpdateOne(i => i.Id == this.Id, Builders<BaseCollect<T>>.Update.Set(i => i.Value, this.Value));
            }
            return collection.UpdateOne(i => i.Id == this.Id, Builders<BaseCollect<T>>.Update.Set(i => i.Value, this.Value));
        }

    }

    class Usr
    {
        public ObjectId Id { get; set; }

        public string label { get; set; }

        public DateTime cre_time { get; set; }

        public double level { get; set; }

        public Dept OfDept { get; set; }

    }
}

 

标签:set,get,MongoDB,netcore,Value,Id,public
From: https://www.cnblogs.com/jonney-wang/p/16609311.html

相关文章

  • Java操作MongoDB采用MongoRepository仓库进行条件查询
    1.实体类1publicclassPersonimplementsSerializable{2privatestaticfinallongserialVersionUID=-8288372263345673367L;3privateStringid;4......
  • golang-mongodb
    0、结构体typeStudentstruct{//_id不用带入NamestringAgeint}1、连接驱动:github.com/mongodb/mongo-go-driver1、普通连接client......
  • golang+mongodb+Aggregate管道查询
    在go语言中使用mongodb管道创建视图并条件分页查询使用bson.D{{}}包裹管道关键字条件例如:go语言中MongoDB配置qmngo查询总页数:bson.D{{"$count","total_cou......
  • MongoDB 单服务器创建用户并授权
    MongoDB单服务器创建用户并授权前言之前使用MongoDB时对于用户的认证和授权一直模模糊糊,各种教程看的半半拉拉,最后勉强能用。现在有时间了来总结一下。基础1.常见......
  • mongoDB学习笔记(正在完善中)
    打开mongoshellmongo添加超级管理员账户useadmin 切换到admin数据库db.createUser({user:'myadmin',pwd:'myadmin',roles:[{role:'userAdminAnyDatabase'......
  • 解析PHP中常见的mongodb查询操作_PHP教程
    //栏位字串为$querys=array("name"=>"shian");//数值等于多少$querys=array("number"=>7);//数值大于多少$querys=array("number"=>array('$gt'=>5));//数值大......
  • mongodb 安装配置
    环境准备#cat/etc/redhat-releaseCentOSLinuxrelease7.9.2009(Core)vim/etc/security/limits.conf*softnproc655350*hardnproc655350*softnofile6......
  • 小白快速在cenos7系统搭建mongodb数据库及compass远程连接
    前言:本人的cenos系统是在腾讯云部署的云服务器,为个人网站提供服务,这里说明一下安装数据库遇到的问题和折腾记录。远程连接云服务器:这一步使用本地系统的可以跳过。之前......
  • 高可用mongodb集群(分片+副本):性能测试
    Yahoo!CloudServingBenchmark(YCSB)是一个Java语言实现的用于云端或者服务器端的数据库性能测试工具,其内部涵盖了常见的NoSQL数据库产品,如Cassandra、MongoDB、HBase......
  • MongoDB基础
    1.业务应用场景1)Highperformance:对数据库高并发读写的需求2)HugeStorage:对海量数据的高效存储和访问的需求3)HighScalability&&HighAvaliablity:对数据的高可扩展性......