MongoDB 聚合函数到 C#
MongoDB Aggregate function to C#
这件事困扰了我一整夜。
我有以下 MongoDb 函数,returns 集合中的热门搜索包括计数
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: 'Command aggregate failed: A
pipeline stage specification object must contain exactly one field..'
这是我的 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);
$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 }
}
这件事困扰了我一整夜。
我有以下 MongoDb 函数,returns 集合中的热门搜索包括计数
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: 'Command aggregate failed: A pipeline stage specification object must contain exactly one field..'
这是我的 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);
$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 }
}