asp.net(C#) 中 怎么使用 MongoDb
1. 先引用以下Dll(如果找不到 到gethub上下载源代码自己编译 特别是MongoDB.Driver.Legacy.dll 我自己找了半天没找到):
MongoDB.Bson.dll
MongoDB.Driver.Core.dll
MongoDB.Driver.dll
MongoDB.Driver.Legacy.dll
2. 以下是测试代码:
using System; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Linq; using MongoDB.Driver.Builders; namespace MongoDbDemo { public class MongoDbHelper { public static void TestMongoDb() { var connectionString = "mongodb://192.168.201.140:27017"; var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase("test"); var collection = database.GetCollection<Entity>("entities"); var entity = new Entity { Name = "Tom" }; collection.Insert(entity); var id = entity.Id; var query = Query<Entity>.EQ(e => e.Id, id); entity = collection.FindOne(query); entity.Name = "Dick"; collection.Save(entity); var update = Update<Entity>.Set(e => e.Name, "Harry"); collection.Update(query, update); collection.Remove(query); } } public class Entity { public ObjectId Id { get; set; } public string Name { get; set; } } }