3

I use mongodb with c# driver.I have a collection which contains subdocument

public class EntityA 
{
    public string Name { get; set; }

    public string Description { get; set; }

    public List<EntityB> BItems { get; set; }
}

public class EntityB
{
    public string BName { get; set; }

    public string BDesc { get; set; }
}

I want to create query and take list of EntityB items

from a in mycollection
where BItems.Any(k => k.BName == entity.Name)
select a.BItems;

I can query nested document but when I retrive the list all other child items come to list how can I take list of BItems to meet up my criteria.I can query subdocument but when I want to take list All Bitems are coming to mylist

1
  • I'd suggest to update post along with sample MongoDB documents and desired document you want to see in response to your query. Commented Apr 13, 2016 at 15:19

3 Answers 3

2

If i understood you correctly you want to return BItems collections when BName is equals to it's parents Name field? If that is the case than it goes like this:

var result = from a in mycollection
             from b in a.BItems
             where a.Name == b.BName
             select b;
Sign up to request clarification or add additional context in comments.

3 Comments

@NikolaLukovic, you have a little error in your query. You should project using b instead of a.BItems
I upgrade my driver to latest version but I still get The SelectMany query operator is not supported error
$project or $group does not support {document} when I want to execute query
1

AS I Understand SelectMany meets to unwind propert in mongodb and to use unwind property we cannot add criteria directly.

MongoDB finding nested objects that meet criteria

       var result = from k in (from a in col.AsQueryable()
                     from b in a.Columns        
                     select b) where k.ColumnName==a.Name select k;

Comments

0

Well, now I tested with MongoDB C# driver 2.2.3 and MongoDB 3.2.1 and indeed SelectMany is already supported, so you can also do this:

 var entities= mycollection.AsQueryable<EntityA>()
                           .SelectMany(e=>e.BItems, (e,b)=>new {entityA=e,bitem=b})
                           .Where(t => t.entityA.Name == t.bitem.Name)
                           .Select(t=>t.bitem)
                           .ToList();

Update

Another possible solution could be fetching first the data and filtering latter in memory:

var entities= mycollection.AsQueryable<EntityA>()
                          .Select(e=>new{e.Name,e.BItems}).ToList();

var result=entities.SelectMany(e=>e.BItems.Where(b=>b.Name==e.Name));

9 Comments

I upgrade my driver to latest version but I still get The SelectMany query operator is not supported error
What version of MongoDB do you have installed?
Maybe is only supported to MongoDB 3.X server version. Did you try with my second solution?
I have to many record,if I try to filter in memory that time i have performance issue
My DB version is 3.07
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.