1.Web.config配置: <configuration> <connectionStrings> <add name="MongoSessionServices" connectionString="mongodb://192.168.2.88:27017" /> </connectionStrings> </configuration> 2.建立BaseDal.cs public class BaseDal { protected static IMongoDatabase MogoDatabase { get { return new MongoClient(ConfigurationManager.ConnectionStrings["MongoSessionServices"].ToString()).GetDatabase("数据库名称"); } } /// <summary> /// MongoDb查询 /// </summary> /// <typeparam name="T">对象名称</typeparam> /// <param name="collectionName">表名</param> /// <returns></returns> protected static IQueryable<T> IQueryableInit<T>(string collectionName) where T : new() { var colls = MogoDatabase.GetCollection<T>(collectionName); var query = from n in colls.AsQueryable() select n; return query; } } 3.建立Model public class Text { //mongodb生成的_id可由下列方式生成 //[BsonId] //public ObjectId Id { get; set; } //时间戳,机器ID,进程ID和序列号组成的12字节值 [BsonId(IdGenerator = typeof(StringObjectIdGenerator))] public string Id { get; set; } //由字符串组成 //[BsonId(IdGenerator = typeof(CounterIdGenerator))] //public int Id { get; set; } //由数字组成 [BsonElement("name")] //mongodb中的名称 public string Name { get; set; } } public class CounterIdGenerator : IIdGenerator { private static int _counter = 0; public object GenerateId(object container, object document) { return _counter++; } public bool IsEmpty(object id) { return id.Equals(default(int)); } } 4.查询 public class GenericArticle_Dal : BaseDal { public static IQueryable<MongoDB_Models.GenericArticle> GetInit() { var query = IQueryableInit<MongoDB_Models.GenericArticle>(MongoDB_Models.Collections.GenericArticle); return query; } } static void Main(string[] args) { var list=GenericArticle_Dal.IQueryable<MongoDB_Models.GenericArticle>().ToList(); } 5.多表查询 public class Video_Dal { public static IQueryable<VideoExt> GetInit(IMongoCollection<Video> coll1, IMongoCollection<VideoList> coll2) { var temp = from n in coll1.AsQueryable() join f in coll2.AsQueryable() on n.ID equals f.ID select new HeavenVideoExt() { Id = n.Id, ModelId = n.ModelId, AddMongoDateTime = n.AddMongoDateTime, ID = n.ID, Title = n.Title, UploadImgPath = n.UploadImgPath, VideoList = f }; return temp; } } var colls1 = MogoDatabase.GetCollection<Video>("Video"); var colls2 = MogoDatabase.GetCollection<VideoList>("VideoList"); var list = Video_Dal.GetInit(colls1, colls2).ToList();