2
我有一個這樣的文件:過濾器+ ElemMatch
{
"File" : "xxxxxxx.txt",
"Content" : [
{ "tag" : "Book",
"name" : "TestBook1",
"value" : "xxx"
},
{ "tag" : "Dept",
"name" : "TestDept1",
"value" : "yyy"
},
{ "tag" : "Employee",
"name" : "TestEmployee1",
"value" : "zzz"
}]
}
用下面的代碼
var subFilter = Builders<BsonDocument>.Filter.Eq("tag", "Dept");
var filter = Builders<BsonDocument>.Filter.ElemMatch("Content", subFilter);
var result =
collection.Find(filter)
.Project(Builders<BsonDocument>.Projection.Exclude("_id").Include("Content.$"))
.ToList();
我能得到
{
"tag" : "Dept",
"name" : "TestDept1",
"value" : "yyy"
}
,我應該怎麼做,如果我還需要過濾File
字段?
所以我的查詢是這樣的:
If File = "xxxxxx.txt" and Content.tag = "Dept"
試試這個。 var andFilter = subFilter&filter var result = collection.Find(andFilter).Project(Builders .Projection.Exclude(「_ id」).include(「Content。$」)) .ToList(); –
notionquest
什麼驅動程序版本? – profesor79