1

i am trying to fetch element based on a property of a sub collection.

     var filter = Builders<User>.Filter.ElemMatch(x => x.BusinessRequestStatuses, x => x.RequestId == requestId);
        var res = await collection.Find(filter).ToListAsync();

and document structure for "User" looks like this

enter image description here

BusinessRequestStatuses is array of custom type

        public List<BusinessRequestStatus> BusinessRequestStatuses { get; set; }

and BusinessRequestStatus is as below

public class BusinessRequestStatus
{
    public string RequestId { get; set; }
    public RequestStatus Status { get; set; }
}

i am setting up a proper requestId that does exists in sub collection (first) but i dont get any result (res) back.

enter image description here

Raas Masood
  • 1,475
  • 3
  • 23
  • 61
  • Have you tried to change `BusinessRequestStatus` into an array ? I suppose that your code builds an expression that is not aware of `_v` in the path. You can check that by enabling MongoDB profiler – mickl Feb 09 '19 at 07:08
  • 1
    _v is something that comes automatically when i add BusinessRequestStatus. looks like its some mangodb thing. – Raas Masood Feb 09 '19 at 07:10
  • 1
    i just confirmed that if i remove _t _v and make it just an array it works. but now i have to think how to avoid _t _v it is caused by this line await testRepository.SetCollection(x => x.Id == businessUser.Id, "users", "BusinessRequestStatuses", listOfBusinessRequests.ToArray()); – Raas Masood Feb 09 '19 at 07:22
  • I've just tried to create simple class with List and it's getting stored without any `_t` fields. I suppose that you have some type casting in your code (probably in the code pasted above) and that brings additional field `_t` which represents original type (List) – mickl Feb 09 '19 at 07:49
  • @RaasMasood could you add your `Insert` or `Update` method for adding a `BusinessRequestStatus`? – Mahdi Feb 22 '19 at 15:12

0 Answers0