实现了 对mongodb数据库的增删改 操作
using System;using System.Collections.Generic;using System.Linq;using System.Web;using MongoDBMVC3.Models;using MongoDB;namespace MongoDBMVC3.Biz{ public class BizModel { public static Channel GetChannelbyID(Guid cid) { using (Mongo mg = new Mongo(DBcon._connectionString)) { mg.Connect(); var db = mg.GetDatabase(DBcon._channel); var list = db.GetCollection<Channel>(); var query = from c in list.Linq() select c; Channel channel = query.Where(c => c.ChannelId == cid).First(); return channel; } } public static List<Channel> GetChannel() { using (Mongo mg = new Mongo(DBcon._connectionString)) { mg.Connect(); var db = mg.GetDatabase(DBcon._channel); var list = db.GetCollection<Channel>(); var query = (from c in list.Linq() select c).ToList<Channel>(); return query; } } public static void AddChannel(Channel model) { using (Mongo mg = new Mongo(DBcon._connectionString)) { mg.Connect(); var db = mg.GetDatabase(DBcon._channel); var list = db.GetCollection<Channel>(); list.Insert(model); } } public static void UpdateChannel(Channel model) { using (Mongo mg = new Mongo(DBcon._connectionString)) { mg.Connect(); var db = mg.GetDatabase(DBcon._channel); var list = db.GetCollection<Channel>(); list.Update(model, c => c.ChannelId == model.ChannelId); } } public static void DeleteChannel(Guid cid) { using (Mongo mg = new Mongo(DBcon._connectionString)) { mg.Connect(); var db = mg.GetDatabase(DBcon._channel); var list = db.GetCollection<Channel>(); list.Remove(c => c.ChannelId == cid); } } }}
评论