简体   繁体   English

MongoDB linq C#选择文档,其中子文档数组包含字符串数组中的所有值

[英]MongoDB linq C# select document where array of subdocumnets contain all values from string array

I have a collection on MongoDb with a similar stricture to the object below. 我在MongoDb上有一个与以下对象相似的集合。

{
    _id: ObjectId("0000000"),
    groupName: "Group A",
    users[
        {
            userId: "1111111",
            rollDescription: "Some Text Here"
        },
        {
            userId: "2222222",
            rollDescription: "Some Text Here"
        },
        {
            userId: "3333333",
            rollDescription: "Some Text Here"
        }
    ]
}

Elsewhere in the system an array of "userIds" is generated and I need to select all groups that contain all userIds in the array. 在系统的其他地方,将生成一个“ userIds”数组,我需要选择包含该数组中所有userIds的所有组。

Is it possible to do this using just linq? 是否可以仅使用linq来做到这一点?

I know I can use this if I had just an array of userIds: 我知道如果我只有一个userIds数组,我可以使用它:

from g in groups.AsQueryable<Group>() where g.Users.ContainsAll(usersIds.ToArray()) select g;

Is there something similar for querying an array of subdocumnets instead of an array of strings? 查询子文档数组而不是字符串数组是否有类似的东西?

Here's how I'd do it in a MongoDB query : 这是我在MongoDB查询中要做的事情:

db.collection.find({ "users.userId": { $all: [ "1111111", "2222222", "3333333" ] }})

I think LINQ will be quite a bit trickier. 我认为LINQ会比较棘手。 Does this work (I don't have MongoDB installed)? 这行得通吗(我没有安装MongoDB)?

from g in groups.AsQueryable<Group>() where g.Users.ConvertAll(u => g.UserId).AsQueryable().ContainsAll(userIds) select g;

Even if it did I suspect that it will not be as performant as using the MongoDB query builder: 即使我怀疑它不会像使用MongoDB查询构建器那样出色:

groups.Find(Query.All("users.userId", userIds))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM