Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDb与查询C#驱动程序不同_Mongodb_Mongodb .net Driver - Fatal编程技术网

MongoDb与查询C#驱动程序不同

MongoDb与查询C#驱动程序不同,mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,我试图使用db.collection.distinct(field,query)命令,文档化。我正试着用C#driver调用它,记录在案 目前我正在使用代码: _repository.Search(item.searchCriteria).Select(i => i.messageId).Distinct().ToList() 其中messageId是一个字符串,搜索函数执行以下操作: //Create search accross all properties of type. pub

我试图使用
db.collection.distinct(field,query)
命令,文档化。我正试着用C#driver调用它,记录在案

目前我正在使用代码:

_repository.Search(item.searchCriteria).Select(i => i.messageId).Distinct().ToList()
其中messageId是一个字符串,搜索函数执行以下操作:

//Create search accross all properties of type.
public IQueryable<SearchType> Search(SearchType entity)
    {
        Type entityType = entity.GetType();
        var propertiesToSearch = entityType.GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
        query = _collection.AsQueryable();
        query = query.WhereAnd(
            query.ElementType,
            propertiesToSearch.Select(p => new SearchCriteria()
                {
                    Column = p.Name,
                    Value = p.GetValue(entity),
                    Operation = WhereOperation.Equal
                }).ToArray());
        return query;
    }
运行此操作时,我遇到以下错误:

“Distinct仅支持单个字段。与Distinct一起使用的投影必须解析为文档中的单个字段。”

我使用的是Mongo 2.4.9和官方的C#driver 1.8.3

方法
.distinct()
方法是一个较旧的实现,更方便地包装mapReduce。对于任何涉及到的简单操作,您都应该使用

因此,外壳等效物:

db.collection.aggregate([
{“$match”:{“$and”:[{“prop1”:”“},{“prop2”:”“}]},
{“$group”:{“\u id”:“$messageId”}
])

这些文档只是作为BSON文档链形成的。有各种各样的例子。

您是否尝试过聚合/组。这应该能满足您的需求。您是否尝试过在没有
搜索功能的情况下进行试验?@Andrew此处的更改是否更适合您的目的?你真的应该在这里使用聚合。这就是重点。谢谢,这正是我想要实现的:)@NeilLunn
distinct
命令在聚合框架之前。。但是,它是在C++中实现的,与MapReduce(REF:)无关。shell中的
distinct()
帮助程序是命令的包装器。
db.collection.distinct("messageId", { $and: [ { prop1: "" }, { prop2: "" } ]  })