gpt4 book ai didi

c# - 过滤+元素匹配

转载 作者:可可西里 更新时间:2023-11-01 10:21:57 25 4
gpt4 key购买 nike

我有这样一个文档:

{ 
"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字段也需要过滤怎么办?

所以我的查询是这样的:

如果 File = "xxxxxx.txt"和 Content.tag = "Dept"

最佳答案

使用驱动程序版本 2.2.3.3

  var fdb = Builders<BsonDocument>.Filter;
var pdb = Builders<BsonDocument>.Projection;
var subFilter = fdb.Eq("tag", "Note");
var filter = fdb.And (
fdb.Eq("File","xxxxxxx.txt"),
fdb.ElemMatch("Content", subFilter));
var result = _collection
.Find(filter)
.Project(pdb.Exclude("_id").Include("Content.$").Include("File"))
.ToList();

关于c# - 过滤+元素匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38258139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com