一、MongoDB的安装
MongoDb在windows下的安装与以auth方式启用服务
二、下载驱动
使用nuget搜索“mongodb”,下载“MongoDB.Driver”(这是官方推荐的一个驱动,完全免费),它会自动下载“MongoDB.Bson”、“MongoDB.Driver.Core”
三、代码编写
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()}"; } } }
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); } } }
然后运行,可以去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(); }
因为商品是随机产生的,所以可能导致你我之间的结果不一样。
接下来我们查询待审核的商品,并显示待审核的商品总数,我们更改下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
接下来我们对第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)); } } }
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 { public ProductComment(string content) { if (content == null) { throw new ArgumentNullException("content"); } Id = ObjectId.GenerateNewId(); Content = content; CheckState = CommentCheckState.WaitingCheck; CreateTime = DateTime.Now; } /// <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()}"; } } }
var beforeUpdateProduct = productCollection.Find(m => m.SaleState == ProductSaleState.WaitingCheck).FirstOrDefault(); Console.WriteLine($"更新前信息{beforeUpdateProduct?.ToString()}"); //注意线程安全,这里只是做演示 beforeUpdateProduct.Comment("哇,这个好好吃啊!"); var updateFilter = Builders<Product>.Update.Set(m => m.SaleState, ProductSaleState.OnSale).Set(m => m.ModifyTime, DateTime.Now).Set(m => m.Comments, beforeUpdateProduct.Comments); var updateResult = productCollection.UpdateOne(m => m.Id == beforeUpdateProduct.Id, updateFilter); if (updateResult.IsModifiedCountAvailable) { var afterUpdateProduct = productCollection.Find(m => m.Id == beforeUpdateProduct.Id).FirstOrDefault(); Console.WriteLine("更新销售状态成功====="); Console.WriteLine($"更新后信息{afterUpdateProduct?.ToString()}"); Console.WriteLine("评论信息:"); afterUpdateProduct.ShowComments(); } else { Console.WriteLine("更新失败====="); }
下一步我们查找有评论待审核的商品列表
var commentWaitingCheckProducts = productCollection.Find(m => m.Comments.Where(k => k.CheckState == CommentCheckState.WaitingCheck).Any()).ToEnumerable(); foreach (var item in commentWaitingCheckProducts) { Console.WriteLine(item.ToString()); }
var projection = Builders<Product>.Projection.Expression(m => new ProductDto { Comments = m.Comments, Id = m.Id, Name = m.Name, Price = m.Price, SaleState = m.SaleState }); var commentWaitingCheckProducts = productCollection.Find(m => m.Comments.Where(k => k.CheckState == CommentCheckState.WaitingCheck).Any()).Project(projection).ToEnumerable(); foreach (var item in commentWaitingCheckProducts) { Console.WriteLine(item.ToString()); }
简单的操作就到这里了,其它的一些操作可以根据文档来,驱动对lambda的支持可以让我们更加容易上手查询一些条件难的查询。