【发布时间】:2019-07-02 19:49:30
【问题描述】:
所以我在文档和子文档上构建了一个过滤器,然后将该过滤器传递给一个不同的查询。我想在光标中使用该查询,但出现错误,无法从使用中推断 FindAsync。尝试明确指定类型参数。
我对 Mongodb 很陌生……任何帮助都会很棒。如果我只是通过过滤器,这将有效,但我只需要不同的文档。
var builder = Builders<newMsg>.Filter;
var filter = builder.Eq("type", "CREATE") & builder.Eq("entities.errorCondition", 14);
// var result = collection.Find(filter).ToList();
IList<newMsg> distinct = collection.Distinct<newMsg>("entities.ID", filter).ToList<newMsg>();
if (distinct.Count > 0)
{
using (IAsyncCursor<newMsg> cursor = await collection.FindAsync(distinct))
{
while (await cursor.MoveNextAsync())
{
IEnumerable<newMsg> batch = cursor.Current;
foreach (newMsg document in batch)
{
//This gives me the entire list as a string
var subDocument = document.Entities;
foreach (var sd in subDocument)
{
//do some stuff
}
}
}
}
}
}
我的类文件是这样的
[BsonIgnoreExtraElements]
internal class newMsg
{
[BsonId]
public ObjectId _id { get; set; }
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("entities")]
public List<entity> Entities { get; set; }
}
[BsonIgnoreExtraElements]
internal class entity : newMsg
{
[BsonElement("errorCondition")]
public double ErrorCondition { get; set; }
[BsonElement("ID")]
public string ID { get; set; }
}
【问题讨论】:
标签: c# mongodb filter distinct