标签:
可能是由于.NET Core还不是正式版的缘故吧,MongoDB的官方Driver(http://mongodb.github.io/mongo-csharp-driver/)一直不支持.NET Core,这给想在.NET Core上尝试MongoDB带来了不便,本人就是其中之一 :)
于是Fork了官方的source code,没有太多的修改,就可以运行了,于是打包发布,不敢独享!
可以在Nuget上搜 ”RaisingStudio.MongoDB.Driver“ 找到这个包,也可以直接用Package Management Console安装:
Install-Package RaisingStudio.MongoDB.Driver
示例:
using MongoDB.Bson; using MongoDB.Driver;
var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("foo"); var collection = database.GetCollection<BsonDocument>("bar"); await collection.InsertOneAsync(new BsonDocument("Name", "Jack")); var list = await collection.Find(new BsonDocument("Name", "Jack")) .ToListAsync(); foreach(var document in list) { Console.WriteLine(document["Name"]); }
或使用强类型方式:
public class Person { public ObjectId Id { get; set; } public string Name { get; set; } }
var client = new MongoClient("mongodb://localhost:27017"); var database = client.GetDatabase("foo"); var collection = database.GetCollection<Person>("bar"); await collection.InsertOneAsync(new Person { Name = "Jack" }); var list = await collection.Find(x => x.Name == "Jack") .ToListAsync(); foreach(var person in list) { Console.WriteLine(person.Name); }
详细说明请参见官方文档:
http://mongodb.github.io/mongo-csharp-driver/
源代码:
https://github.com/zhongzf/mongo-csharp-driver
标签:
原文地址:http://www.cnblogs.com/zhongzf/p/5616971.html