官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads。下载后,还提供了一个酷似msdn的帮助文档。
samus驱动:https://github.com/samus/mongodb-csharp/downloads。
using System; using System.Collections.Generic; using System.Linq; using MongoDB.Driver; using MongoDB.Bson; using MongoDB.Driver.Builders; namespace TestMongoDb { class Program { public static string connectionString = "mongodb://localhost"; //数据库名 private static string databaseName = "TestDb"; static void Main(string[] args) { // 添加一条数据 //Users us = new Users() { Age = 123, Name = "TestNameA", Sex = "F" }; //us.Insert(); // 删除一条数据 //IMongoQuery iq = new QueryDocument("name", "TestNameA"); //Users.Remove(iq); //修改一条数据 //IMongoQuery iq = new QueryDocument("name", "TestNameA"); //IMongoUpdate iu = MongoDB.Driver.Builders.Update.Set("sex", "M").Set("age", 100); //Users.Update(iq, iu); //获取数据列表 //IMongoQuery iq = new QueryDocument("name", "TestNameA"); IMongoQuery iq = Query.And(Query.GTE("age", 40), Query.Matches("name", "/^Test/"));//>40 List<Users> userList = Users.Search(iq).ToList(); foreach (Users item in userList) { Console.WriteLine(item.Name + " " + item.Sex); } } public class Users { private static string tableUser = "Users"; public Users() { } public Users(String name, Int32 age, String sex) { Name = name; Age = age; Sex = sex; } public String Name { get; set; } public Int32 Age { get; set; } public String Sex { get; set; } public Boolean Insert() { BsonDocument dom = new BsonDocument { { "name", Name }, { "age", Age }, {"sex",Sex} }; return MongoHelper.Insert(tableUser, dom); } public static IEnumerable<Users> Search(IMongoQuery query) { foreach (BsonDocument tmp in MongoHelper.Search(tableUser, query)) yield return new Users(tmp["name"].AsString, tmp["age"].AsInt32, tmp["sex"].AsString); } public static Boolean Remove(IMongoQuery query) { return MongoHelper.Remove(tableUser, query); } public static Boolean Update(IMongoQuery query, IMongoUpdate new_doc) { return MongoHelper.Update(tableUser, query, new_doc); } } public static class MongoHelper { public static MongoCursor<BsonDocument> Search(String collectionName, IMongoQuery query) { //定义Mongo服务 MongoServer server = MongoServer.Create(connectionString); //获取databaseName对应的数据库,不存在则自动创建 MongoDatabase mongoDatabase = server.GetDatabase(databaseName); MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName); try { if (query == null) return collection.FindAll(); else return collection.Find(query); } finally { server.Disconnect(); } } /// <summary> /// 新增 /// </summary> public static Boolean Insert(String collectionName, BsonDocument document) { MongoServer server = MongoServer.Create(connectionString); MongoDatabase mongoDatabase = server.GetDatabase(databaseName); MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName); try { collection.Insert(document); server.Disconnect(); return true; } catch { server.Disconnect(); return false; } } /// <summary> /// 修改 /// </summary> public static Boolean Update(String collectionName, IMongoQuery query, IMongoUpdate new_doc) { MongoServer server = MongoServer.Create(connectionString); MongoDatabase mongoDatabase = server.GetDatabase(databaseName); MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName); try { collection.Update(query, new_doc); server.Disconnect(); return true; } catch { server.Disconnect(); return false; } } /// <summary> /// 移除 /// </summary> public static Boolean Remove(String collectionName, IMongoQuery query) { MongoServer server = MongoServer.Create(connectionString); MongoDatabase mongoDatabase = server.GetDatabase(databaseName); MongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>(collectionName); try { collection.Remove(query); server.Disconnect(); return true; } catch { server.Disconnect(); return false; } } } } }
时间: 2024-12-28 20:24:29