概述
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。
在C#中想使用MongoDB我们还需要下载驱动,现在一般比较常见的驱动有官网驱动,samus驱动。samus驱动除了支持一般形式的操作之外,还支持linq方式操纵数据。本文所使用的为官网驱动。官网驱动地址:点击下载
下载完后我们在下载包中可以得到二个DLL:
MongoDB.Driver.dll 驱动程序
MongoDB.Bson.dll 序列化、Json相关
实例代码
新增:
//数据库连接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //数据库 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //新增 student stud_Add = new student() { name="赵云", code="2001", sex="女", age="23" }; collection.Insert<student>(stud_Add); //查询 QueryDocument query_sel = new QueryDocument { { "name", "赵云" } }; var list = collection.FindAs<student>(query_sel); foreach (student item in list) { Console.WriteLine("name:{0},code:{1}", item.name, item.code); }
显示结果:
修改:
//数据库连接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //数据库 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //更新 QueryDocument query_upd = new QueryDocument { { "name", "赵云" } }; IMongoUpdate update = Update.Set("code", "4444"); collection.Update(query_upd, update);
显示结果:
删除:
//数据库连接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //数据库 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //删除 QueryDocument query_del = new QueryDocument { { "name", "赵云" } }; collection.Remove(query_del);
全量代码:
static void Main(string[] args) { //数据库连接字符串 const string connectionString = "mongodb://127.0.0.1:27017"; //数据库 const string Database = "ck_test_db"; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(Database); MongoCollection collection = db.GetCollection("student"); //新增 student stud_Add = new student() { name="赵云", code="2001", sex="女", age="23" }; collection.Insert<student>(stud_Add); //更新 QueryDocument query_upd = new QueryDocument { { "name", "赵云" } }; IMongoUpdate update = Update.Set("code", "4444"); collection.Update(query_upd, update); //删除 QueryDocument query_del = new QueryDocument { { "name", "赵云" } }; collection.Remove(query_del); //查询 QueryDocument query_sel = new QueryDocument { { "name", "赵云" } }; var list = collection.FindAs<student>(query_sel); foreach (student item in list) { Console.WriteLine("name:{0},code:{1}", item.name, item.code); } } public class student { public ObjectId _id; //BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId public string name { get; set; } public string code { get; set; } public string sex { get; set; } public string age { get; set; } /*_id 属性必须要有,否则在更新数据时会报错:“Element '_id' does not match any field or property of class”。*/ }
基础的增删改查就是上面所述。
实例代码:点击下载