[英]Query MongoDB in C# using LINQ
我有一个简单的类,代表MongoDB文档中的字段
class Measurement
{
public ObjectId id { get; set; }
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
我正在尝试使用以下条件获取符合我条件的文件
var collection = database.GetCollection<Measurement>(mongoCollectionName);
var query = from a in collection.AsQueryable<Measurement>()
where a.dt > 100
select a;
取消条件后,我确实会收到所有文件,但没有条件。 回应说没有相符的文件,但是有(例如dt = 1538555)
查询看起来像这样{aggregate([{“ $ match”:{“ dt”:{“ $ gt”:100}}}]))}。 我使用此线程和mongodb文档MongoDB C#Aggregation with LINQ的响应构建示例
我很可能解决了我的愚蠢错误,将不胜感激
我不再直接使用c#驱动程序,所以我的解决方案是使用MongoDAL。
using System;
using System.Linq;
using MongoDAL;
namespace Example
{
class Measurement : Entity
{
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
class Program
{
static void Main(string[] args)
{
new DB("measurements");
var measurement1 = new Measurement
{
s = 10,
ml = 20,
dt = 100,
p = new int[] { 1, 2, 3, 4, 5 }
};
var measurement2 = new Measurement
{
s = 11,
ml = 22,
dt = 200,
p = new int[] { 1, 2, 3, 4, 5 }
};
measurement1.Save();
measurement2.Save();
var result = (from m in DB.Collection<Measurement>()
where m.dt > 100
select m).ToArray();
Console.ReadKey();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.