关闭

NET使用Mongodb入门示例

标签: mongodbnet入门
127人阅读 评论(0) 收藏 举报
分类:

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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:895016次
    • 积分:13021
    • 等级:
    • 排名:第1092名
    • 原创:307篇
    • 转载:439篇
    • 译文:56篇
    • 评论:93条
    博客专栏
    文章分类
    打赏
    如果你觉得我的文章对您有用,请随意打赏。 微信 支付宝