Projection in Where Clause Query of a Embedded document in MongoDB Collection using C#

前端 未结 3 771
悲&欢浪女
悲&欢浪女 2021-01-23 10:00

Filter the Collection in DB instead of Memory

I\'m having a Model Class, Save it in a MongoDB Collection then Query the same as per my expec

3条回答
  •  遇见更好的自我
    2021-01-23 10:16

    EDIT

    Added projection - so selected array contains only documents where IsLive==true

    I think it is easier to use typed queries as c# is strongly typed language. I used ElemMatch as this is designed to scan an array and looks for a matching element.

    var filterDef = new FilterDefinitionBuilder();
    var filter = filterDef.Eq(x => x.IsLive, true);
    
    var projectDef = new ProjectionDefinitionBuilder();
    var projection = projectDef.ElemMatch("EmpMobile", "{IsLive:true}");            
    
    var empList = collectionEmpInfo.Find(filter).Project(projection).ToList();
    

提交回复
热议问题