[英]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.