MongoDB聚合函数到C#
这整晚都在困扰着我 我有下面的MongoDb函数,它返回集合中包含there计数的顶部搜索MongoDB聚合函数到C#,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,这整晚都在困扰着我 我有下面的MongoDb函数,它返回集合中包含there计数的顶部搜索 db.search.aggregate([ { $match: { keywords: { $not: {$size: 0} } } }, { $unwind: "$term" }, { $group: { _id: {$toLower: '$term'},
db.search.aggregate([
{
$match: {
keywords: { $not: {$size: 0} }
}
},
{ $unwind: "$term" },
{
$group: {
_id: {$toLower: '$term'},
count: { $sum: 1 }
}
},
{
$match: {
count: { $gte: 2 }
}
},
{ $sort : { count : -1} },
{ $limit : 100 }
]);
我试图将其移动到C#函数,我得到了错误:
MongoDB.Driver.MongoCommandException:'命令聚合失败:A
管道阶段规范对象必须仅包含一个字段..'
这是我的C#版本,有人能看到我遗漏了什么吗
var pipeline = new BsonDocument[] {
new BsonDocument
{
{
"$match",
new BsonDocument {{"keywords", new BsonDocument {{"$not", new BsonDocument {{"$size", 0}}}}}}
}
},
new BsonDocument {{"$unwind", "$term"}},
new BsonDocument
{
{
"$group", new BsonDocument
{
{"_id", new BsonDocument {{"$toLower", "$term"}}},
{
"count", new BsonDocument
{
{"$sum", 1}
}
}
}
}
},
new BsonDocument
{
{
"$match", new BsonDocument
{
{"count", new BsonDocument {{"$gte", 2}}}
}
},
{
"$sort", new BsonDocument
{
{"count", -1}
}
},
{"$limit", 100}
}
};
var result = collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
Console.WriteLine(result);
var管道=新的BsonDocument[]{
新的B文件
{
{
“$match”,
新BsonDocument{{“关键字”,新BsonDocument{{“$not”,新BsonDocument{{“$size”,0}}
}
},
新的BsonDocument{{“$unwind”,“$term”},
新的B文件
{
{
“$组”,新的B文件
{
{{{{{{$toLower,“$term”}}新的B文件,
{
“计数”,新的B记录文件
{
{“$sum”,1}
}
}
}
}
},
新的B文件
{
{
“$match”,新的B文件
{
{“计数”,新的BsonDocument{{“$gte”,2}}
}
},
{
“$sort”,新的BsonDocument
{
{“计数”,-1}
}
},
{“$limit”,100}
}
};
var result=collection.Aggregate(pipeline.ToListAsync();
控制台写入线(结果);
$match
,$sort
和$limit
应该是单独的聚合管道阶段。在您的情况下,他们有一个根BsonDocument
,请尝试:
...
new BsonDocument
{
{
"$match", new BsonDocument
{
{"count", new BsonDocument {{"$gte", 2}}}
}
}
},
new BsonDocument()
{
{ "$sort", new BsonDocument
{
{"count", -1}
}
}
},
new BsonDocument()
{
{ "$limit", 100 }
}
你可能会感兴趣,而不是上面那些无法阅读/无法维护的混乱。