MongoDB C#驱动程序抛出的DistinctAsync无法反序列化';列表<;字符串>';从BsonType';字符串';
我有一个文档,它有一个字符串标记列表。我正在寻找整个集合中所有不同的标记作为字符串列表返回MongoDB C#驱动程序抛出的DistinctAsync无法反序列化';列表<;字符串>';从BsonType';字符串';,mongodb,.net-core,mongodb-query,mongodb-.net-driver,Mongodb,.net Core,Mongodb Query,Mongodb .net Driver,我有一个文档,它有一个字符串标记列表。我正在寻找整个集合中所有不同的标记作为字符串列表返回 public class TestGetDistinctDocument { public string Id { get; set; } public string Name { get; set; } public string Description { get; set; } public List<strin
public class TestGetDistinctDocument
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<string> Tags { get; set; }
}
返回集合中所有不同“名称”的列表。但是对标签做同样的操作会抛出
System.FormatException:无法从BsonType“String”反序列化“List”。
这是引发异常的代码:
var cursor = await collection.DistinctAsync(doc => doc.Tags, filter);
var distinctTags = await cursor.ToListAsync();
return distinctTags.SelectMany(tag => tag).ToList();
我正在使用mongo csharp驱动程序版本2.8 使用
FieldDefinition
定义要“区分”的字段
fielddefinitionfield=“标记”代码>而不是使用Linq作为use
var filter = FilterDefinition<TestGetDistinctDocument>.Empty;
FieldDefinition<TestGetDistinctDocument, string> field = "Tags";
这给出了所有TestGetDistinctDocument中不同标记的列表。这里有一种实现方法
使用FieldDefinition
定义要“区分”的字段
fielddefinitionfield=“标记”代码>而不是使用Linq作为use
var filter = FilterDefinition<TestGetDistinctDocument>.Empty;
FieldDefinition<TestGetDistinctDocument, string> field = "Tags";
这给出了所有TestGetDistinctDocument中不同标记的列表。您能显示此集合的记录吗?也许你在tags字段上有一个字符串值而不是数组。tags肯定是一个数组,我确定了,你能显示这个集合的记录吗?也许你在tags字段上有一个字符串值,而不是数组。tags肯定是数组,我保证了这一点
var cursor = collection.DistinctAsync(field, FilterDefinition<TestGetDistinctDocument>.Empty);
return await cursor.Result.ToListAsync();