Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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
C#MongoDb直接从JSON运行聚合查询_C#_Mongodb - Fatal编程技术网

C#MongoDb直接从JSON运行聚合查询

C#MongoDb直接从JSON运行聚合查询,c#,mongodb,C#,Mongodb,我正在研究一种服务方法,它可以从json输入运行mongodb聚合查询。其思想是使用构建器生成查询,将该查询转换为json,将其传递给要反序列化并运行的服务。对于查找查询,我可以使用Bson文档 public string DoGenericFind(string queryDoc, string collectionName) { BsonDocument document = BsonSerializer.Deserialize<BsonDocument

我正在研究一种服务方法,它可以从json输入运行mongodb聚合查询。其思想是使用构建器生成查询,将该查询转换为json,将其传递给要反序列化并运行的服务。对于查找查询,我可以使用Bson文档

    public string DoGenericFind(string queryDoc, string collectionName)
    {
        BsonDocument document = BsonSerializer.Deserialize<BsonDocument>(queryDoc);

        var results = _context.Database.GetCollection<dynamic>(collectionName).FindSync<BsonDocument>(document);

        if (results == null)
            return null;
        else 
            return results.ToList().ToJson();
    }
公共字符串DoGenericFind(字符串queryDoc,字符串collectionName)
{
BsonDocument document=BsonSerializer.Deserialize(queryDoc);
var results=\u context.Database.GetCollection(collectionName).FindSync(document);
如果(结果==null)
返回null;
其他的
返回results.ToList().ToJson();
}

我很难找到类似的方法来处理聚合。我在这里找到的唯一的例子就是尝试做一些类似于他们传递某种BsonDocument[]的事情。但是,我的驱动程序版本(2.5)的intellisense说我需要传递一个pipelineDefinition,我找不到如何使用它的好例子。

在发布此内容后立即找到了一个好例子

这让我的最终解决方案看起来像

    public string DoGenericAggregate(string queryDoc, string collectionName)
    {
        var query = BsonSerializer.Deserialize<BsonDocument[]>(queryDoc).ToList();

        List<BsonDocument> list;
        using (var cursor = _context.Database.GetCollection<dynamic>(collectionName).Aggregate<BsonDocument>(query))
        {
            list = cursor.ToList();
        }

        if (list == null)
            return null;
        else
            return list.ToJson();
    }
公共字符串DoGenericAggregate(字符串queryDoc,字符串collectionName)
{
var query=BsonSerializer.Deserialize(queryDoc.ToList();
名单;
使用(var cursor=_context.Database.GetCollection(collectionName.Aggregate(query))
{
list=cursor.ToList();
}
if(list==null)
返回null;
其他的
return list.ToJson();
}

我很确定我也有类似的疯狂行为,但也有错误。但是无论如何,
列表
可以隐式转换为
管道定义
,这样就可以通过聚合函数了。

请查看此MongoDB驱动程序文档页面:

在主题中,PIPELINE有一个简单的示例可以帮助您。 要使用管道定义,您需要创建这样一个简单的代码

PipelineDefinition pipeline = new BsonDocument[] 
{
    new BsonDocument { { "$match", new BsonDocument("x", 1) } },
    new BsonDocument { { "$sort", new BsonDocument("y", 1) } }
};

col.aggregate(pipeline);

希望这有帮助

我看着这完全相同的页面,可以发誓我通过了同样的东西。一定是在周五周末的阴霾中。所有的开发者都会这样!在最新版本的驱动程序中,它现在是PipelineDefinition pipeline=newBSondocument[]{}我在Mongo的Jira中打开的。