Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
MongoDB find method returns className

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!