gpt4 book ai didi

c# - 在 Mongo Linq 查询中执行相交的机制是什么

转载 作者:行者123 更新时间:2023-12-04 01:53:20 24 4
gpt4 key购买 nike

如何在 Linq 中为 Mongo 编写 where 子句以确定本地集合的任何成员是否包含在我的文档中的集合中。

即(这是我期望的工作,但没有)

var myLocalList = <PopulateMyLocalList>;

var myDocs = db.GetCollection<MyDoc>("MyDocs").AsQueryable();
var result = myDocs.Where(d => d.MyCollection.Intersect(myLocalList).Any());

所以假设 Mongo Linq 提供者不支持这个 - 我该怎么做?

最佳答案

在 MongoDB 语法中有一个 $in当您想要将内存中的数组与嵌入在文档中的另一个数组相匹配时,它的工作方式与 intersect+any 完全相同。

If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. , , etc.)



在 MongoDB C# 驱动程序中,您可以使用 AnyIn将该运算符应用于两个数组。尝试:
db.col.save({ Collection: [1,2,3] })l

然后在 C# 中:
var filterBuilder = Builders<YourModel>.Filter;
var inMemoryList = new List<int>() { 3, 4, 5 };

var result = Col.Find(filterBuilder.AnyIn(x => x.Collection, inMemoryList)).ToList();

关于c# - 在 Mongo Linq 查询中执行相交的机制是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959853/

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