C # & MongoDb - How to make filter in child document?

0

I'm starting my adventures in MongoDB and I'm having a problem specifically with document filtering. The template for my application:

public class MatrizCurricular
{
    [BsonId]     
    public ObjectId _id { get; set; }
    public string Curso { get; set; }       
    public IList<Semestre> Semestres { get; set; }
}
public class Semestre
{
    public int NumSemestre { get; set; }     
    public IList<Disciplina> Disciplinas { get; set; }
}
public class Disciplina
{
    [BsonId]
    public ObjectId _id { get; set; }
    public string Nome { get; set; }

}

As each MatrizCurricular , has its own disciplines, I make a query within it by a discipline that has that same _id

//Crio uma consulta           
var filter = Builders<MatrizCurricular>.Filter.Eq("semestres.disciplinas._id", new ObjectId(id));
//Executo a consulta
var results = bd.MatrizCurricular.Find(filter).ToList();

Result:

[
{
    "_id": "sdfsdfsdf",
    "curso": "Administração",
    "semestres": [
        {
            "numSemestre": 1,                
            "disciplinas": [
                {
                    "_id": "asdasdasd",
                    "nome": "Administração financeira"
                },
                {
                    "_id": "paeiutaiurt",
                    "nome": "Teoria Geral da Administração"
                },
                {
                    "_id": "ajshgdajsh",
                    "nome": "Matemática"
                }
            ]
        },
        {
            "numSemestre": 2,            
            "disciplinas": [
                {
                    "_id": "6hti7d3dt7dwht67",
                    "nome": "Macroeconomia"
                },
                {
                    "_id": "d8u9dm7or7yu4t4e6",
                    "nome": "Raciocínio lógico"
                },
                {
                    "_id": "s8ory7niw3i7hn",
                    "nome": "Teoria da organização"
                }
            ]
        }
    ]
}
]

The problem is that when I run this query, the result I have is the entire MatrizCurricular document that contains the Discipline you want, but with all other < strong> Discipline and Semester relatives. I can work around the situation by making a query using Linq after the data has already been fetched from the database, but this is clearly not the best option.

Is there a way to bring a document of the type Discipline to the bank directly?

    
asked by anonymous 03.03.2018 / 21:51

0 answers