Mongo:将查询结果转换为自定义类

时间:2021-08-17 23:59:58

1、自定义类

    public class MyClass
{
public string Name { get; set; }
public int Corners { get; set; }
}

2、查询数据并转换

   var config = new MongoConfigurationBuilder();
config.ConnectionString("");
using (Mongo mongo = new Mongo(config.BuildConfiguration()))
{
mongo.Connect();
try
{
var db = mongo.GetDatabase("MyTest");
var list = db.GetCollection<MyClass>("MyClass").Linq().ToList<MyClass>(); var superclass =
db.GetCollection<MyClass>("MyClass").Linq().Where(p => p.Corners > ).ToList<MyClass>();
}
finally
{
mongo.Disconnect();
}
}