如何使用C#过滤MongoDB数组以仅返回所需的特定记录
我的问题如何使用C#过滤MongoDB数组以仅返回所需的特定记录,c#,arrays,mongodb,C#,Arrays,Mongodb,我的问题 public List<T> GetETrannsfer<T>(string table, string Branch, string userName, string status) { var collection = db.GetCollection<T>(table); var filter = Builders<T>.Filter.And(Builders<T
public List<T> GetETrannsfer<T>(string table, string Branch, string userName, string status)
{
var collection = db.GetCollection<T>(table);
var filter = Builders<T>.Filter.And(Builders<T>.Filter.Regex("Branch", new BsonRegularExpression("/^" + Branch + "$/i")),
Builders<T>.Filter.Eq("eLogInfo.userName", userName),
Builders<T>.Filter.Eq("eLogInfo.status", status));
return collection.Find(filter).ToList();
}
如果数组中的一个对象包含过滤变量,MongoDB将向我发送数组中所有对象的结果。
我只需要包含特定变量的对象
因此,我尝试按建议使用ElemMatch,但仍然得到相同的结果记录未被筛选结果包含特定数组下的所有记录
public EquipmentLogModel GetETrannsfer(string table, string Branch, string userName, string status)
{
var collection = db.GetCollection<EquipmentLogModel>(table);
var filter = Builders<EquipmentLogModel>.Filter.And(Builders<EquipmentLogModel>.Filter.Regex("Branch", new BsonRegularExpression("/^" + Branch + "$/i")),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.status == status),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.userName == userName));
return collection.Find(filter).First();
}
public EquipmentLogModel GeteTransfer(字符串表、字符串分支、字符串用户名、字符串状态)
{
var collection=db.GetCollection(表);
var filter=Builders.filter.And(Builders.filter.Regex(“Branch”,新的BsonRegularExpression(“/^”+Branch+“$/i”),
Builders.Filter.ElemMatch(x=>x.eLogInfo,x=>x.status==status),
Builders.Filter.ElemMatch(x=>x.eLogInfo,x=>x.userName==userName));
返回collection.Find(filter.First();
}
我还尝试了下面的方法,它只返回了一条记录,我需要多条记录,并且我无法通过多个变量筛选返回
public EquipmentLogInfoModel GetETrannsfer(string table, string Branch, string userName, string status)
{
var filter = Builders<EquipmentLogModel>.Filter.And(Builders<EquipmentLogModel>.Filter.Regex("Branch", new BsonRegularExpression("/^" + Branch + "$/i")),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.status == status),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.userName == userName));
var collection = db.GetCollection<EquipmentLogModel>(table).Find(filter)
.FirstOrDefault();
return collection.eLogInfo.FirstOrDefault(a => a.userName == userName);
}
public EquipmentLoginModel GeteTransfer(字符串表、字符串分支、字符串用户名、字符串状态)
{
var filter=Builders.filter.And(Builders.filter.Regex(“Branch”,新的BsonRegularExpression(“/^”+Branch+“$/i”),
Builders.Filter.ElemMatch(x=>x.eLogInfo,x=>x.status==status),
Builders.Filter.ElemMatch(x=>x.eLogInfo,x=>x.userName==userName));
var collection=db.GetCollection(表).Find(过滤器)
.FirstOrDefault();
return collection.eLogInfo.FirstOrDefault(a=>a.userName==userName);
}
使用elemMatch。这里:嗨,我试着这么做Builders.Filter.ElemMatch(x=>x.eLogInfo,x=>x._id==id));但是T不包含eLogInfo的定义,我不知道如何使ElemMatch与我使用的语法一起工作。错误是T不包含eLogInfoI可以从T切换到实际模型的定义,但这表示我无法从我的模型转换到T。请使用上面的示例方法,并向我展示如何使用ELEMMATCH的示例请参见我尝试的代码上面的更新。当你说,“包含特定变量的对象。”您的意思是您只需要特定字段吗?如果需要,您是否尝试过像collection.Find(filter.Project)(Builders.Projection.Exclude(“_id”)。Include(“”)。ToList()这样的“投影”;
public EquipmentLogModel GetETrannsfer(string table, string Branch, string userName, string status)
{
var collection = db.GetCollection<EquipmentLogModel>(table);
var filter = Builders<EquipmentLogModel>.Filter.And(Builders<EquipmentLogModel>.Filter.Regex("Branch", new BsonRegularExpression("/^" + Branch + "$/i")),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.status == status),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.userName == userName));
return collection.Find(filter).First();
}
public EquipmentLogInfoModel GetETrannsfer(string table, string Branch, string userName, string status)
{
var filter = Builders<EquipmentLogModel>.Filter.And(Builders<EquipmentLogModel>.Filter.Regex("Branch", new BsonRegularExpression("/^" + Branch + "$/i")),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.status == status),
Builders<EquipmentLogModel>.Filter.ElemMatch(x => x.eLogInfo, x => x.userName == userName));
var collection = db.GetCollection<EquipmentLogModel>(table).Find(filter)
.FirstOrDefault();
return collection.eLogInfo.FirstOrDefault(a => a.userName == userName);
}