C# MongoDB 增删改查 代码示例 基于MongoDB 2.4.1 驱动DLL

Anonymity | | 访问(319)
using Clump.MongoDB.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
 
namespace Clump.MongoDB._2._4.Controllers
{
    public class HomeController : Controller
    {
        // 模型集合
        private static IMongoCollection<messageinfo> collection;
 
        public HomeController()
        {
            //"mongodb://localhost:27017"
            MongoClient client = new MongoClient("mongodb://clump:clump@localhost:27017/woplus");//"mongodb://sa(admin):123456@localhost:27017"
            IMongoDatabase database = client.GetDatabase("woplus");
            collection = database.GetCollection<messageinfo>("NavigationInfo");
        }
 
        // 列表
        public ActionResult Index()
        {
            string message = "";
            List<messageinfo> list = collection.Find(o => o.UserID > 0).ToList();
            message = "Count:" + list.Count();
            foreach (var item in list)
            {
                message += "<br>ID:" + item.JsonId + ",UserID:" + item.UserID + ",Category:" + item.Category + ",Title:" + item.Title;
            }
            return Content(message);
        }
 
        // 查询单个实体
        public ActionResult Single(string id)
        {
            string message = "";
            ObjectId oid;
            try
            {
                oid = new ObjectId(id);
            }
            catch (Exception)
            {
                throw new ArgumentNullException("传入的主健 ObjectID 值格式不正确,请检查!");
            }
            MessageInfo entity = collection.Find(o => o.UserID > 0).First();
            message += "ID:" + entity.JsonId + ",UserID:" + entity.UserID + ",Category:" + entity.Category + ",Title:" + entity.Title;
            return Content(message);
        }
 
        // 添加数据
        public ActionResult Insert()
        {
            string message = "成功";
 
            MessageInfo mg = null;
            for (int i = 0; i < 10; i++)
            {
                mg = new MessageInfo();
                mg.UserID = 1;
                mg.Category = 1;
                mg.Title = "标题" + i;
                mg.Content = "内容";
                mg.ReadStatus = 0;
                mg.CreateTime = DateTime.Now;
                mg.UpdateTime = DateTime.Now;
                collection.InsertOne(mg);
            }
 
            return Content(message);
        }
 
        // 更改数据
        public ActionResult Update(string id, string title = "")
        {
            string message = "";
            ObjectId oid = new ObjectId(id);
            var filter = Builders<messageinfo>.Filter.Eq("Id", oid);// 检索条件
            var update = Builders<messageinfo>.Update.Set(o => o.Title, title).Set(o => o.UserID, 2).Set(o => o.Category, 2);// 更改项
            var updateOneResult = collection.UpdateOne(filter, update);
            if (updateOneResult.IsAcknowledged)
            {
                message += "更改成功,";
            }
 
            MessageInfo entity = collection.Find(o => o.Id == oid).First();
            message += "ID:" + entity.JsonId + ",UserID:" + entity.UserID + ",Category:" + entity.Category + ",Title:" + entity.Title;
            return Content(message);
        }
 
        // 更改数据
        public ActionResult Replace(string id, string title = "")
        {
            string message = "";
            ObjectId oid = new ObjectId(id);
            var filter = Builders<messageinfo>.Filter.Eq("Id", oid);// 检索条件
            MessageInfo entity = collection.Find(o => o.Id == oid).First();
            entity.Title = title;
            entity.UserID = 2;
            entity.Category = 2;
            var updateOneResult = collection.ReplaceOne(filter, entity);
            if (updateOneResult.IsAcknowledged)
            {
                message += "更改成功,";
            }
 
 
            message += "ID:" + entity.JsonId + ",UserID:" + entity.UserID + ",Category:" + entity.Category + ",Title:" + entity.Title;
            return Content(message);
        }
 
        // 删除单个数据
        public ActionResult Delete(string id)
        {
            string message = "";
            ObjectId oid = new ObjectId(id);
            var deleteResult = collection.DeleteOne(o => o.Id == oid);
            if (deleteResult.IsAcknowledged)
            {
                message += "删除成功,";
            }
 
            return Content(message);
        }
 
        // 删除多条数据
        public ActionResult DeleteMany()
        {
            string message = "";
            var deleteResult = collection.DeleteMany(o => o.UserID > 0);
            if (deleteResult.IsAcknowledged)
            {
                message += "删除成功,";
            }
            return Content(message);
        }
 
    }
}

using System;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using System.Web.Script.Serialization;
 
namespace Clump.MongoDB.Models
{

    // 站内消息
    [Serializable]
    public partial class MessageInfo
    {
    
        //  Id
        private ObjectId _id;
 
    
        // Id编号 可使用它来作为查询条件和输出
        [ScriptIgnore]
        public ObjectId Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
 
    
        // JsonId 仅用于输出_id的string,并不能使用她来作为查询条件
        [BsonIgnore]
        public string JsonId
        {
            get
            {
                return Id.ToString();
            }
            set
            {
                Id = new ObjectId(value);
            }
        }
 
    
        // 用户ID
        public int UserID { get; set; }
 
    
        // 消息分类
        public int Category { get; set; }
 
    
        // 标题
        public string Title { get; set; }
 
    
        // 内容
        public string Content { get; set; }
 
    
        // 阅读状态[0:未阅读][1:已阅读]
        public int ReadStatus { get; set; }
 
    
        // 创建时间
        private DateTime _createtime = DateTime.Now;
 
    
        // 创建时间
        public DateTime CreateTime
        {
            get
            {
                return _createtime;
            }
            set
            {
                _createtime = value;
            }
        }
 
    
        // 最后修改时间
        public DateTime UpdateTime { get; set; }
 
    }
}