Mongo University answer to an exercise
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
namespace homework2
{
class Program
{
static void Main(string[] args)
{
MainAsync(args).Wait();
Console.WriteLine("press enter");
Console.ReadLine();
}
private static async Task MainAsync(string[] args)
{
var connectionstring = "mongodb://127.0.0.1:27018";
var client = new MongoClient(connectionstring);
var db = client.GetDatabase("mongowork");
var col = db.GetCollection("students");
var group = new BsonDocument
{
{ "$group",
new BsonDocument
{
{ "_id", new BsonDocument
{
{
"student_id","$student_id"
}
}
},
{
"minQuantity", new BsonDocument
{
{
"$push", "$$ROOT"
}
}
}
}
}
};
var x = await col.Aggregate()
.Match(y => y.type == "homework")
.Group(new BsonDocument{{"_id", "$student_id"}, {"minQuantity", new BsonDocument("$min", "$score")}})//, {"test", new BsonDocument("$push","$$ROOT")}})
.SortBy(y => y.score)
.ToListAsync();
/* var filter = Builders.Filter.Eq("_id", "$student_id","minQuantity", new BsonDocument("$min", "score")});*/
/*var pipeline = new[] { group };
var result = await col.Find(group).ToListAsync();*/
foreach (var t in x)
{
await col.DeleteManyAsync(doc => doc.score.Equals(t.minQuantity));
Console.WriteLine(t.student_id + t._id.ToString());
}
}
public class students
{
[BsonElement]
public object minQuantity
{
get;
set;
}
[BsonElement]
public object test
{
get;
set;
}
[BsonElement]
public int _id
{
get; set;
}
[BsonElement]
public int student_id
{
get;
set;
}
[BsonElement]
public string type
{
get;
set;
}
[BsonElement]
public double score
{
get;
set;
}
}
}
}