gpt4 book ai didi

mongodb - 获取 listOfStrings 中 subDocument.value 所在的所有项目

转载 作者:可可西里 更新时间:2023-11-01 09:13:23 25 4
gpt4 key购买 nike

我在 dotnetcore 2.1 中使用 MongoDB.Driver nuget 包。我正在尝试返回集合中的文档列表,其中子文档字段等于我拥有的列表中包含的任何项目。理想情况下,我需要在 C# 语法中为 dotnetcore 2.1 的 MongoDB.Driver nuget 包使用它。

文档
{
“_id”:“e29628a65e914c1e91b3fd9cbf6f2353”,
“启用”:真,
“名称”:“Document123”,
"DisplayName": "一些文档",
“描述”:“一些描述”,
“数据” : [
“姓”,
“电子邮件”,
“名”,
“邮政编码”
],
“项目” : [
{
“_id”:“1”,
“名字”:“鲍勃”
},
{
“_id”:“2”,
“名称”:“史密斯”
}
]
}

如果这是 SQL,这就是我正在尝试做的:


选择 *
来自文档 a,项目 b
其中 a.Id = b.DocumentId 和
b.Name IN ('bob', 'smith', 'alex')

这是我们所拥有的不能与 MongoDB 驱动程序一起工作的东西:

string[] names = new [] { "bob", "smith", "alex" };
var document = new BsonDocument()
{
new BsonElement("Items.Name", new BsonDocument()
{
new BsonElement("$in", new BsonArray(names))
})
};
var itemsQuery = collection
.Aggregate()
.Match(document)
;

var items = itemsQuery.ToList();

提前致谢。

最佳答案

事实证明,我们必须“展开”并在此之后运行匹配查询。这是对我们有用的代码。

        var collection = GetCollection<Document>();

var document = new BsonDocument()
{
new BsonElement("Name", "Document123"),
new BsonElement("Items.Name", new BsonDocument()
{
new BsonElement("$in", new BsonArray(new [] { "bob", "smith", "alex"}))
})
};

var itemsQuery = collection
.Aggregate()
.Unwind(d => d.Items)
.Match(document)
;

var items = itemsQuery.ToList();

关于mongodb - 获取 listOfStrings 中 subDocument.value 所在的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663113/

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