Projeção na consulta da cláusula Where de um documento incorporado na coleção MongoDB usando C #
Filtrar a coleção no banco de dados em vez da memória
Estou tendo uma classe de modelo, salve-a em uma coleção do MongoDB e, em seguida, consulte o mesmo conforme a minha expectativa mencionada abaixo.
Minha classe de modelo:
public Class Employee
{
public ObjectId Id { get; set; }
public string EmpID { get; set; }
public string EmpName { get; set; }
public List<Mobile> EmpMobile { get; set; }
public bool IsLive { get; set; }
}
public Class Mobile
{
public string MobID { get; set; }
public string MobNumber { get; set; }
public bool IsPreferred { get; set; }
public bool IsLive { get; set; }
}
Os valores são
List<Employee> EmpInfo = new List<Employee>() {
new Employee()
{
EmpID = "100",
EmpName = "John",
EmpMobile = new List<Mobile>()
{
new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = false },
new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true },
},
IsLive = true
},
new Employee()
{
EmpID = "101",
EmpName = "Peter",
EmpMobile = new List<Mobile>()
{
new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = false },
new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = false },
},
IsLive = true
},
new Employee()
{
EmpID = "102",
EmpName = "Jack",
EmpMobile = new List<Mobile>()
{
new Mobile() { MobNumber = "55566610", IsPreferred = true, IsLive = true },
new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true },
},
IsLive = false
}
}
collectionEmpInfo.InsertMany(EmpInfo);
var empList = collectionEmpInfo.Find(new BsonDocument()).ToList();
Agora desejo selecionar apenasEmpInfo.IsLive == true
dentro do documento incorporado eu preciso apenasEmpInfo.EmpMobile.IsLive == true
documentos móveis satisfeitos
Minha saída prevista:
List<Employee> EmpInfo = new List<Employee>() {
new Employee()
{
EmpID = "100",
EmpName = "John",
EmpMobile = new List<Mobile>()
{
new Mobile() { MobNumber = "55566611", IsPreferred = false, IsLive = true }
},
IsLive = true
},
new Employee()
{
EmpID = "101",
EmpName = "Peter",
EmpMobile = new List<Mobile>()
{
},
IsLive = true
}
}
Por favor, me ajude a escrever umaConsulta da Cláusula Where para minha saída esperada usandoc # MongoDB.
Nota: Filtrar a coleção no banco de dados em vez da memória
Minhas bibliotecas e conexões do MongoDB são
IMongoClient _client = new MongoClient();
IMongoDatabase _database = _client.GetDatabase("Test");