目标:对于使用C#.NET和MongoDB v4.0进行最快的查询,请使用单个聚合管道样式查询对具有嵌套数组记录的文档执行多个不同查询,并返回一个展平的集。
>当前问题:对于第一级文档条目,当在嵌套数组记录上运行非重复记录时,效果很好,出乎意料。
示例文档:
{
"title" : "Movie A",
"released": 1984,
"minutes" : 90,
"category": "SciFi",
"cost": 24000000,
"gross": 48000000,
"rated": "PG",
"score": 3.4,
"cast" : [
{
"name": "Buzz",
"gender":"M",
"age": 28,
"country": "USA",
"role": "Leading",
"award" : "Golden Globe",
"pay": 500000
},
{
"name": "Sally",
"gender":"F",
"age": 21,
"country": "Austrailia",
"role": "Supporting",
"award" : "Academy",
"pay": 300000
}
]
}
如何使用Mongo Client构建:
使用;
db.createCollection("movies");
db.movies.insertOne({ "title" : "Movie A", "released": 1984, "minutes": 90, "category": "SciFi", "cost": 24000000, "gross": 48000000, "rated": "PG", "score": 3.4, "cast" : [ { "name": "Buzz", "gender":"M", "age": 28, "country": "USA", "role": "Leading", "award" : "Golden Globe", "pay": 500000 }, { "name": "Sally", "gender":"F", "age": 21, "country": "Austrailia", "role": "Supporting", "award": "Academy", "pay": 300000 } ]});
db.movies.insertOne({ "title" : "Movie B", "released": 1986, "minutes": 120, "category": "Horror", "cost": 28000000, "gross": 40000000, "rated": "R", "score": 4.4, "cast" : [ { "name": "Ryan", "gender":"M", "age": 55, "country": "England", "role": "Leading", "award": "Oscar Nomination", "pay": 100000 }, { "name": "Sally", "gender":"F", "age": 23, "country": "Austrailia", "role": "Supporting", "award": "Academy", "pay": 300000 } ]});
db.movies.insertOne({ "title" : "Movie C", "released": 1988, "minutes": 106, "category": "Drama", "cost": 38000000, "gross": 45000000, "rated": "PG13", "score": 2.4, "cast" : [ { "name": "Pat", "gender":"F", "age": 42, "country": "Ireland", "role": "Supporting", "award": "Irish", "pay": 350000 }, { "name": "Gene", "gender":"F", "age": 44, "country": "France", "role": "Leading", "award": "Independent", "pay": 280000 } ]});
带有Mongo v 4.0和2.5驱动程序版本的C#.NET代码
MongoDatabase mdb = Mongo.Controller.Instance.getDB();
var collection = mdb.GetCollection("movies");
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{ "released", new BsonDocument
{
{"$gte", 1984},
{ "$lte", 1988}
}
}
}
}
};
var group = new BsonDocument
{
{ "$group",
new BsonDocument {
{ "_id", 0},
{ "categoryDistinct", new BsonDocument { { "$addToSet", "$category" } }},
{ "ratedDistinct", new BsonDocument { { "$addToSet", "$rated" } }},
{ "countryDistinct", new BsonDocument { { "$addToSet", "$cast.country" } }},
{ "awardDistinct", new BsonDocument { { "$addToSet", "$cast.award" } }}
}
}
};
var pipeline = new[] { match, group };
var args = new AggregateArgs();
args.Pipeline = pipeline;
args.AllowDiskUse = true;
var results = collection.Aggregate(args).ToList();
foreach (var obj in results)
{
Console.WriteLine(obj.ToString());
}
输出:
{ "_id" : 0, "categoryDistinct" : ["Drama", "Horror", "SciFi"], "ratedDistinct" : ["PG13", "R", "PG"], "countryDistinct" : [["Ireland", "France"], ["England", "Austrailia"], ["USA", "Austrailia"]], "awardDistinct" : [["Irish", "Independent"], ["Oscar Nomination", "Academy"], ["Golden Globe", "Academy"]] }
注意:categoryDistinct和ratedDistinct会如我所愿地返回,但是countryDistinct和awardDistinct却没有,它们以嵌套的子数组记录形式返回。
所需的输出:countryDistinct应该是
["Austrailia,England,France,Ireland,USA"]
awardDistinct should have been: ["Academy","Golden Globe","Independent","Irish","Oscar Nomination"];
(I know I didn't specify a sort anywhere so if you can give a pointer that will not impact speed, that would be greatly appreciated.
任何有关如何有效执行此操作的指示-自觉地提高速度将不胜感激。
预先感谢, 肖恩