NET使用Mongodb入门示例

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongodbTest.Infrastructure;

namespace MongodbTest
{
    class Program
    {
        private static Random rand = new Random();
        private static MongoHelper mongohelper = new MongoHelper();
        static void Main(string[] args)
        {

            Add(mongohelper.database);

            Console.WriteLine("Hello World!");
        }

        static void Add(IMongoDatabase mongoDb)
        {
            var Collection = mongoDb.GetCollection<Infrastructure.OAApprovalProcess<dynamic>>("OAApprovalProcess");
            var apply = new Infrastructure.OAApprovalProcess<dynamic>()
            {
                Applicant = new Infrastructure.Applicant
                {
                    BranchCompany = "东莞分公司",
                    BranchCompanyId = 1,
                    DepartmentId = 1,
                    DepartmentName = "技术部",
                    Id = 1,
                    Name = "1360000000",
                    PositionId = 1,
                    PositionName = "开发人员",
                    UserImage = null,
                    UserName = "李XX"
                },
                Approvals = new List<Infrastructure.Approver> {
                    new Infrastructure.Approver{
                        BranchCompany="东莞分公司",
                        DepartmentName="技术部",
                        Id=2,
                        Name="1370000000",
                        PositionName="开发人员",
                        UserName="李XX",
                        ApprovalDate=null,
                        ApprovalOrder=1,
                        Group=1,
                        ImageSignature=null,
                        InvalidDescription=null,
                        InvalidTime=null,
                        ProcessStatus=0,
                        Remarks=null
                    },
                    new Infrastructure.Approver{
                        BranchCompany="东莞分公司",
                        DepartmentName="技术部",
                        Id=2,
                        Name="1370000000",
                        PositionName="开发人员",
                        UserName="李XX",
                        ApprovalDate=null,
                        ApprovalOrder=1,
                        Group=1,
                        ImageSignature=null,
                        InvalidDescription=null,
                        InvalidTime=null,
                        ProcessStatus=0,
                        Remarks=null
                    }
                },
                CarbonCopies = new List<Infrastructure.CarbonCopy> {
                    new Infrastructure.CarbonCopy{
                        BranchCompany="东莞分公司",
                        DepartmentName="技术部",
                        Id=2,
                        Name="1370000000",
                        PositionName="开发人员",
                        UserName="李XX",
                        InvalidDescription=null,
                        InvalidTime=null,
                        IsCanInvalid=false,
                        IsHadRead=false,
                        IsInvalid=false
                    },
                    new Infrastructure.CarbonCopy{
                        BranchCompany="东莞分公司",
                        DepartmentName="技术部",
                        Id=2,
                        Name="1370000000",
                        PositionName="开发人员",
                        UserName="李XX",
                        InvalidDescription=null,
                        InvalidTime=null,
                        IsCanInvalid=false,
                        IsHadRead=false,
                        IsInvalid=false
                    }
                },
                ContentAnnex = new List<Infrastructure.ContentAnnex> {
                    new Infrastructure.ContentAnnex{
                        Address="http://wwww.bidu.ocm",
                        FileName="视频",
                        FileType=1
                    }
                },
                SubstitutePerson = new List<Infrastructure.SubstitutePerson> {
                    new Infrastructure.SubstitutePerson{
                        BranchCompany="东莞分公司",
                        DepartmentName="技术部",
                        Name="1370000000",
                        PositionName="开发人员",
                        UserName="李XX",
                        ApprovalDate=null,
                        ApprovalOrder=1,
                        ImageSignature=null,
                        InvalidDescription=null,
                        InvalidTime=null,
                        ProcessStatus=0,
                        Remarks=null,
                        StaffId=5,
                        SubstituteId=5
                    }
                },
                ProcessStatus = 0,
                IsCancel = false,
                CreateDate = DateTime.Now,
            };
            switch (rand.Next(1, 4))
            {
                case 1:
                    apply.ApplyContent = null;
                    apply.ApplyNumber = null;
                    apply.ApplyType = 1;
                    break;
                case 2:
                    apply.ApplyContent = null;
                    apply.ApplyNumber = null;
                    apply.ApplyType = 1;
                    break;
                case 3:
                    apply.ApplyContent = null;
                    apply.ApplyNumber = null;
                    apply.ApplyType = 1;
                    break;
            }

            mongohelper.Insert(apply);

        }
    }
}

MongoHelper.cs

using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// Mongod帮助类
    /// </summary>
    public class MongoHelper : IDisposable
    {
        private const string connStr = "mongodb://127.0.0.1";
        private const string dbName = "OA";

        MongoClient client;
        public IMongoDatabase database;

        public MongoHelper()
        {
            this.client = new MongoClient(connStr);
            this.database = client.GetDatabase(dbName);
        }

        public IMongoCollection<t> GetCollection<t>(string tbName) where t : class
        {
            return database.GetCollection<t>(tbName);
        }



        #region 保存
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public void Insert(OAApprovalProcess<dynamic> t)
        {
            var collection = this.GetCollection<Infrastructure.OAApprovalProcess<dynamic>>(dbName);//.Insert<Infrastructure.OAApprovalProcess<dynamic>>(t);
            collection.InsertOne(t);
        }

        protected void BatchInsert(IList<OAApprovalProcess<dynamic>> list)
        {
            var collection = this.GetCollection<OAApprovalProcess<dynamic>>(dbName);
            collection.InsertMany(list);
        }

        #endregion

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}

OAApprovalProcess.cs实体类

using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using Newtonsoft.Json;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// OA审批流程
    /// </summary>
    public class OAApprovalProcess<applyContent>
    {
        /// <summary>
        /// Id
        /// </summary>
        public BsonObjectId Id { set; get; }

        /// <summary>
        /// 申请编号
        /// </summary>
        public string ApplyNumber { set; get; }

        /// <summary>
        /// 申请类型 
        /// 1广告宣传用品申请表 2广告宣传用品领用签收确认单 3人力需求申请表(增员) 4人力需求申请表(补员)
        /// 5备用金借款申请表 6 业务招待申请 7出差申请表 8请休假申请表 9差旅费用报销表 10普通费用报销表
        /// 11采购申请表 12固定资采购申请表 13特殊费用申请
        /// </summary>
        public int ApplyType { set; get; }

        /// <summary>
        /// 申请者资料
        /// </summary>
        public Applicant Applicant { set; get; }

        /// <summary>
        /// 申请资料
        /// </summary>
        public applyContent ApplyContent { set; get; }

        /// <summary>
        /// 申请资料附件
        /// </summary>
        public List<ContentAnnex> ContentAnnex { set; get; }

        /// <summary>
        /// 审核状态 0审核中 1己通过 2驳回 3作废
        /// </summary>
        public int ProcessStatus { set; get; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [MongoDB.Bson.Serialization.Attributes.BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateDate { set; get; }

        /// <summary>
        /// 审批人
        /// </summary>
        public List<Approver> Approvals { set; get; }

        /// <summary>
        /// 抄送人
        /// </summary>
        public List<CarbonCopy> CarbonCopies { set; get; }

        /// <summary>
        /// 代签人
        /// </summary>
        public List<SubstitutePerson> SubstitutePerson { set; get; }

        /// <summary>
        /// 是否取消
        /// </summary>
        public bool IsCancel { set; get; }
    }
}

Applicant.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// 申请人
    /// </summary>
    public class Applicant
    {
        /// <summary>
        /// 申请人Id
        /// </summary>
        public int Id { set; get; }

        /// <summary>
        /// 申请人账号
        /// </summary>
        public string UserName { set; get; }

        /// <summary>
        /// 申请人昵称
        /// </summary>
        public string Name { set; get; }

        /// <summary>
        /// 用户头像
        /// </summary>
        public byte[] UserImage { set; get; }

        /// <summary>
        /// 分公司名称
        /// </summary>
        public string BranchCompany { set; get; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { set; get; }

        /// <summary>
        /// 职位名称
        /// </summary>
        public string PositionName { set; get; }

        /// <summary>
        /// 分公司名称
        /// </summary>
        public int BranchCompanyId { set; get; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public int DepartmentId { set; get; }

        /// <summary>
        /// 职位名称
        /// </summary>
        public int PositionId { set; get; }
    }
}

ContentAnnex.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// 内容附近
    /// </summary>
    public class ContentAnnex
    {
        /// <summary>
        /// 文件类型(1 图片文件 2其它文件)
        /// </summary>
        public int FileType { set; get; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { set; get; }

        /// <summary>
        /// 文件地址
        /// </summary>
        public string Address { set; get; }
    }
}

Approver.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// 审批人
    /// </summary>
    public class Approver
    {
        /// <summary>
        /// 审批人Id
        /// </summary>
        public int Id { set; get; }

        /// <summary>
        /// 审批人账号
        /// </summary>
        public string UserName { set; get; }

        /// <summary>
        /// 审批人昵称
        /// </summary>
        public string Name { set; get; }

        /// <summary>
        /// 分组
        /// </summary>
        public int Group { set; get; }

        /// <summary>
        /// 审批排序,数值越大,审批越靠后
        /// </summary>
        public int ApprovalOrder { set; get; }

        /// <summary>
        /// 分公司名称
        /// </summary>
        public string BranchCompany { set; get; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { set; get; }

        /// <summary>
        /// 职位名称
        /// </summary>
        public string PositionName { set; get; }

        /// <summary>
        /// 审核状态 0审核中 1己通过 2驳回 3作废
        /// </summary>
        public int ProcessStatus { set; get; }

        /// <summary>
        /// 图片签名
        /// </summary>
        public byte[] ImageSignature { set; get; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { set; get; }

        /// <summary>
        /// 审批时间
        /// </summary>
        public string ApprovalDate { set; get; }

        /// <summary>
        /// 作废时间
        /// </summary>
        public string InvalidTime { set; get; }

        /// <summary>
        /// 作废说明
        /// </summary>
        public string InvalidDescription { set; get; }
    }
}

CarbonCopy.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// 抄送人
    /// </summary>
    public class CarbonCopy
    {
        /// <summary>
        /// 抄送人Id
        /// </summary>
        public int Id { set; get; }

        /// <summary>
        /// 抄送人能否作废审请
        /// </summary>
        public bool IsCanInvalid { set; get; }

        /// <summary>
        /// 是否作废
        /// </summary>
        public bool IsInvalid { set; get; }

        /// <summary>
        /// 作废时间
        /// </summary>
        public DateTime? InvalidTime { set; get; }

        /// <summary>
        /// 作废说明
        /// </summary>
        public string InvalidDescription { set; get; }

        /// <summary>
        /// 抄送人账号
        /// </summary>
        public string UserName { set; get; }

        /// <summary>
        /// 抄送人昵称
        /// </summary>
        public string Name { set; get; }

        /// <summary>
        /// 分公司名称
        /// </summary>
        public string BranchCompany { set; get; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { set; get; }

        /// <summary>
        /// 职位名称
        /// </summary>
        public string PositionName { set; get; }

        /// <summary>
        /// 是否己阅读
        /// </summary>
        public bool IsHadRead { set; get; }
    }
}

SubstitutePerson.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MongodbTest.Infrastructure
{
    /// <summary>
    /// 代签人
    /// </summary>
    public class SubstitutePerson
    {
        /// <summary>
        /// 员工Id
        /// </summary>
        public int StaffId { set; get; }

        /// <summary>
        /// 代签人Id
        /// </summary>
        public int SubstituteId { set; get; }

        /// <summary>
        /// 代签人账号
        /// </summary>
        public string UserName { set; get; }

        /// <summary>
        /// 代签人昵称
        /// </summary>
        public string Name { set; get; }

        /// <summary>
        /// 审批排序,数值越大,审批越靠后
        /// </summary>
        public int ApprovalOrder { set; get; }

        /// <summary>
        /// 分公司名称
        /// </summary>
        public string BranchCompany { set; get; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { set; get; }

        /// <summary>
        /// 职位名称
        /// </summary>
        public string PositionName { set; get; }

        /// <summary>
        /// 审核状态 0审核中 1己通过 2驳回 3作废
        /// </summary>
        public int ProcessStatus { set; get; }

        /// <summary>
        /// 图片签名
        /// </summary>
        public byte[] ImageSignature { set; get; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { set; get; }

        /// <summary>
        /// 审批时间
        /// </summary>
        public string ApprovalDate { set; get; }

        /// <summary>
        /// 作废时间
        /// </summary>
        public string InvalidTime { set; get; }

        /// <summary>
        /// 作废说明
        /// </summary>
        public string InvalidDescription { set; get; }
    }
}

运行结果如图:

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值