• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#简单操作MongoDB_2.4

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、MongoDB的安装

  MongoDb在windows下的安装与以auth方式启用服务

二、下载驱动

  使用nuget搜索“mongodb”,下载“MongoDB.Driver”(这是官方推荐的一个驱动,完全免费),它会自动下载“MongoDB.Bson”、“MongoDB.Driver.Core”

  

  Api文档地址

 三、代码编写

  1、新建四个类:商品、商品销售状态枚举、商品评论、商品评论审核状态枚举

  
using System.ComponentModel;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2017 yjq 版权所有。
    /// 类名:ProductSaleState.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:商品销售状态
    /// 创建标识:yjq 2017/5/21 0:36:02
    /// </summary>
    public enum ProductSaleState
    {
        /// <summary>
        /// 待审核
        /// </summary>
        [Description("待审核")]
        WaitingCheck = 1,

        /// <summary>
        /// 上架
        /// </summary>
        [Description("上架")]
        OnSale = 2,

        /// <summary>
        /// 下架
        /// </summary>
        [Description("下架")]
        OffShelves = 3,

        /// <summary>
        /// 已销售
        /// </summary>
        [Description("已销售")]
        Saled = 4
    }
}


using System.ComponentModel;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2017 yjq 版权所有。
    /// 类名:CommentCheckState.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:评论审核状态
    /// 创建标识:yjq 2017/5/21 0:51:43
    /// </summary>
    public enum CommentCheckState
    {
        /// <summary>
        /// 待审核
        /// </summary>
        [Description("待审核")]
        WaitingCheck = 1,

        /// <summary>
        /// 审核通过
        /// </summary>
        [Description("审核通过")]
        Passed = 2,

        /// <summary>
        /// 审核不通过
        /// </summary>
        [Description("审核不通过")]
        NotPass = 3
    }
}


using Infrastructure;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2015 备胎 版权所有。
    /// 类名:Product.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:商品
    /// 创建标识:yjq 2017/5/18 14:59:35
    /// </summary>
    public sealed class Product
    {
        public Product()
        {
        }

        public Product(string name, decimal price) : this()
        {
            Id = ObjectId.GenerateNewId();
            Name = name;
            Price = price;
            SaleState = ProductSaleState.WaitingCheck;
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 商品ID
        /// </summary>
        [BsonElement(elementName: "_id")]
        public ObjectId Id { get; set; }

        /// <summary>
        /// 商品名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal? Price { get; set; }

        /// <summary>
        /// 销售状态
        /// </summary>
        public ProductSaleState SaleState { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 商品评论
        /// </summary>
        public List<ProductComment> Comments { get; set; }

        public override string ToString()
        {
            return $"{Id}:{Name},价格{Price}元,审核状态{SaleState.Desc()}";
        }
    }
}


using Infrastructure;

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2015 备胎 版权所有。
    /// 类名:ProductComment.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:商品评论
    /// 创建标识:yjq 2017/5/18 15:08:32
    /// </summary>
    public sealed class ProductComment
    {
        /// <summary>
        /// 评论ID
        /// </summary>
        [BsonElement(elementName: "_id")]
        public ObjectId Id { get; set; }

        /// <summary>
        /// 评论内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 评论审核状态
        /// </summary>
        public CommentCheckState CheckState { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? ModifyTime { get; set; }

        public override string ToString()
        {
            return $"评论信息:{Content},审核状态:{CheckState.Desc()}";
        }
    }
}
商品、商品评论

  2、我们先进行新增一个苹果,价格为5.2,且审核状态为待审核的,然后在查询商品列表,输出所有的商品,并显示出其状态

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

namespace MongoDbTest
{
    class Program
    {
        private static string _MongoDbConnectionStr = "mongodb://yjq:123456@localhost:27017/admin";

        static void Main(string[] args)
        {
            var productCollection = GetCollection<Product>();
            //添加一个待审核的商品
            Product product = new Product("苹果", (decimal)5.20);
            productCollection.InsertOne(product);
            Console.WriteLine($"添加商品:{product.ToString()}成功。");
            var productList = productCollection.Find(new BsonDocument()).ToList();
            foreach (var item in productList)
            {
                Console.WriteLine(item.ToString());
            }

            Console.Read();


        }

        private static IMongoCollection<T> GetCollection<T>(string collectionName = null)
        {
            MongoUrl mongoUrl = new MongoUrl(_MongoDbConnectionStr);
            var mongoClient = new MongoClient(mongoUrl);
            var database = mongoClient.GetDatabase(mongoUrl.DatabaseName);
            return database.GetCollection<T>(collectionName ?? typeof(T).Name);
        }
    }
}
运行代码

  

  用robomongodb打开,然后查看对应信息,我们会发现数据库里面存储的时间比我们的当前时间晚8小时,这是因为在安装mongodb的时候,默认的时区不是我们本地的时区导致的,但是只要在时间字段上标记[BsonDateTimeOptions(Kind = DateTimeKind.Local)]就可以在输出的时候显示我们本地时间了。

  

 

 

到这里,我们就完成了简单的新增和查询功能,接下来我们先随机插入几个审核通过、不通过、待审核的商品共100个。

代码如下:

 更改product代码

  
using Infrastructure;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2015 备胎 版权所有。
    /// 类名:Product.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:商品
    /// 创建标识:yjq 2017/5/18 14:59:35
    /// </summary>
    public sealed class Product
    {
        public Product()
        {
        }

        public Product(string name, decimal price) : this(name, price, ProductSaleState.WaitingCheck)
        {
        }

        public Product(string name, decimal price, ProductSaleState saleState)
        {
            Id = ObjectId.GenerateNewId();
            Name = name;
            Price = price;
            SaleState = saleState;
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 商品ID
        /// </summary>
        [BsonElement(elementName: "_id")]
        public ObjectId Id { get; set; }

        /// <summary>
        /// 商品名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal? Price { get; set; }

        /// <summary>
        /// 销售状态
        /// </summary>
        public ProductSaleState SaleState { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 商品评论
        /// </summary>
        public List<ProductComment> Comments { get; set; }

        public override string ToString()
        {
            return $"{Id}:{Name},价格{Price}元,审核状态{SaleState.Desc()}";
        }
    }
}
Product
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using Models;
using MongoDB.Bson;

namespace MongoDbTest
{
    class Program
    {
        private static string _MongoDbConnectionStr = "mongodb://yjq:123456@localhost:27017/admin";

        static void Main(string[] args)
        {
            var productCollection = GetCollection<Product>();
            //添加一个待审核的商品
            //Product product = new Product("苹果", (decimal)5.20);
            //productCollection.InsertOne(product);
            //Console.WriteLine($"添加商品:{product.ToString()}成功。");

            //批量增加商品
            List<Product> productAddList = new List<Product>();
            for (int i = 0; i < 100; i++)
            {
                productAddList.Add(GetRandomProduct());
            }
            productCollection.InsertMany(productAddList);
            var productList = productCollection.Find(new BsonDocument()).ToList();
            foreach (var item in productList)
            {
                Console.WriteLine(item.ToString());
            }

            Console.Read();


        }

        private static IMongoCollection<T> GetCollection<T>(string collectionName = null)
        {
            MongoUrl mongoUrl = new MongoUrl(_MongoDbConnectionStr);
            var mongoClient = new MongoClient(mongoUrl);
            var database = mongoClient.GetDatabase(mongoUrl.DatabaseName);
            return database.GetCollection<T>(collectionName ?? typeof(T).Name);
        }

        private static string[] _ProductNames = new string[] { "苹果", "香蕉", "菠萝", "哈密瓜", "西瓜", "黄瓜", "草莓", "桃子", "芒果", "猕猴桃", "" };
        private static Random rn = new Random();
        private static Product GetRandomProduct()
        {
            var i = rn.Next(_ProductNames.Length);
            decimal price = i * 15;
            var enumValue = rn.Next(1, 5);
            return new Product(_ProductNames[i], price, (ProductSaleState)enumValue);
        }
    }
}
Program

然后运行,可以去robo查看结果,发现数据库里面总共有101条数据

批量增加的操作也执行了,那么接下来我们就继续执行分页和修改删除的功能。

首先我们先查询返回总记录数和前20条商品信息的内容:

  
static void Main(string[] args)
        {
            var productCollection = GetCollection<Product>();
            //添加一个待审核的商品
            //Product product = new Product("苹果", (decimal)5.20);
            //productCollection.InsertOne(product);
            //Console.WriteLine($"添加商品:{product.ToString()}成功。");

            //批量增加商品
            //List<Product> productAddList = new List<Product>();
            //for (int i = 0; i < 100; i++)
            //{
            //    productAddList.Add(GetRandomProduct());
            //}
            //productCollection.InsertMany(productAddList);
            //var productList = productCollection.Find(new BsonDocument()).ToList();
            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.ToString());
            //}
            FilterDefinition<Product> filter = new BsonDocument();
            long productAllCount = productCollection.Count(filter);
            var productList = productCollection.Find(filter).Skip(0).Limit(20).ToList();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"总记录数为{productAllCount.ToString()}");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("前20条商品信息为:");
            Console.ForegroundColor = ConsoleColor.White;
            foreach (var item in productList)
            {
                Console.WriteLine(item.ToString());
            }
            Console.Read();


        }
Program

因为商品是随机产生的,所以可能导致你我之间的结果不一样。

接下来我们查询待审核的商品,并显示待审核的商品总数,我们更改下filte(使用lambda表达式树比较方便),二选一都可以

  
Expression<Func<Product, bool>> expression = m => m.SaleState == ProductSaleState.WaitingCheck;
            long productAllCount = productCollection.Count(expression);
            var productList = productCollection.Find(expression).Skip(0).Limit(20).ToList();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"总记录数为{productAllCount.ToString()}");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("前20条商品信息为:");
            Console.ForegroundColor = ConsoleColor.White;
            foreach (var item in productList)
            {
                Console.WriteLine(item.ToString());
            }
待审核商品
#region 待审核 filter构建
            var filter = Builders<Product>.Filter.Eq("SaleState", ProductSaleState.WaitingCheck);
            long productAllCount = productCollection.Count(filter);
            var productList = productCollection.Find(filter).Skip(0).Limit(20).ToList();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"总记录数为{productAllCount.ToString()}");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("前20条商品信息为:");
            Console.ForegroundColor = ConsoleColor.White;
            foreach (var item in productList)
            {
                Console.WriteLine(item.ToString());
            }
            #endregion
filter 构建条件

接下来我们对第1条待审核的商品进行审核通过的操作,并增加一条“哇,这个好好吃啊!”的评论。

  
using Infrastructure;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2015 备胎 版权所有。
    /// 类名:Product.cs
    /// 类属性:公共类(非静态)
    /// 类功能描述:商品
    /// 创建标识:yjq 2017/5/18 14:59:35
    /// </summary>
    public sealed class Product
    {
        public Product()
        {
        }

        public Product(string name, decimal price) : this(name, price, ProductSaleState.WaitingCheck)
        {
        }

        public Product(string name, decimal price, ProductSaleState saleState)
        {
            Id = ObjectId.GenerateNewId();
            Name = name;
            Price = price;
            SaleState = saleState;
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 商品ID
        /// </summary>
        [BsonElement(elementName: "_id")]
        public ObjectId Id { get; set; }

        /// <summary>
        /// 商品名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal? Price { get; set; }

        /// <summary>
        /// 销售状态
        /// </summary>
        public ProductSaleState SaleState { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 商品评论
        /// </summary>
        public List<ProductComment> Comments { get; set; }

        public override string ToString()
        {
            return $"{Id}:{Name},价格{Price}元,审核状态{SaleState.Desc()}";
        }

        public void ShowComments()
        {
            if (Comments != null)
            {
                foreach (var item in Comments)
                {
                    Console.WriteLine(item.ToString());
                }
            }
            
        }

        public void Comment(string content)
        {
            if (Comments == null)
            {
                Comments = new List<Models.ProductComment>();
            }
            Comments.Add(new Models.ProductComment(content));
        }
    }
}
Product类的更改
  
using Infrastructure;

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace Models
{
    /// <summary>
    /// Copyright (C) 2015 备胎 版权所有。
     

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DropDownList绑定、c#字符串处理、CheckBoxList取值发布时间:2022-07-10
下一篇:
C#判别系统版本以及Win10的识别办法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap