C# MongoDB Distinct Query Syntax

青春壹個敷衍的年華 提交于 2019-11-28 04:32:21

问题


I am trying to get the distinct values from a field in MongoDB. I am having real trouble with the Syntax. Using mongoshell it's relatively easy to do, this is the query I run:

db.cmstest.distinct("categories")

This query returns an array of strings with all the distinct values.

Now I am trying to get the syntax right using the latest official MongoDB Drivers, but not to much success. This is my code, which is unsuccessful:

var categoriesList = await blogContext.Articles.DistinctAsync<List<string>>("categories", "");

Mind you categories is a List<string>.

Could anyone help shed some light? I've tried looking both in the documentation and online and haven't found much.

Thank you in advance.


回答1:


You could try the following approach:

var filter = new BsonDocument();
var categoriesList = await blogContext.Articles.DistinctAsync<string>("categories", filter);


来源:https://stackoverflow.com/questions/35725113/c-sharp-mongodb-distinct-query-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!