I'm a newbie in mongodb. I search for a solution in stackoverflow but i didn't find a solution for me. I can't see my json data. Here is my code block:
class Score { public string type { get; set; }
public double score { get; set; }
}
class Student
{
public int _id { get; set; }
public string name { get; set; }
public List<Score> scores { get; set; }
}
class Program
{
static void Main(string[] args)
{
MainAsync(args).Wait();
Console.WriteLine();
Console.WriteLine("press enter");
Console.ReadLine();
}
static async Task MainAsync(string[] args)
{
var client = new MongoClient();
var db = client.GetDatabase("school");
var col = db.GetCollection<Student>("students");
var filter = Builders<Student>.Filter.Eq("scores.type", "homework");
await col.Find(filter)
.ForEachAsync(c => Console.WriteLine(c));
}
}
It shows 20 columns which is the correct count for my search but all of them are m101.Student. How can I fix this?
You miss the projection. You need to create a projection builder and append it to your Find method as follows:
var filter = Builders<Student>.Filter.Eq("scores.type", "homework");
var projection = Builders<Student>.Projection.Include("scores.$");
await col.Find(filter)
.Project(projection)
.ForEachAsync(c => Console.WriteLine(c));