user3146613
user3146613

Reputation: 11

Return the whole subdocument array from MongoDB

I need to get all of the subdocuments array from the Courses class where the User.UserId = whatever and Courses.Status = active.

Public Class User
    Public Property UserId As String 'this is unique so i would like to index this, unless you think otherwise
    Public Property CourseData() As List(Of Courses) ' 
    Public Property Groups As List(Of String)   
    Public Property BU As List(Of String)     
End Class

Public Class Courses
    Public Property id As String 'this can be dynamic
    Public Property description As String
    Public Property CompletionDate As String
    Public Property Hours As String
    Public Property Status As String
End Class

Using vb.net, I tried a few ways, I only want the courses returned that have a Status="Active" to be dumped into an IEnumerable.

I tried (_users is a collection of User) (_uid is a variable passed into it)

Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")
    
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")

None seem to work, they usually come back with the fields from class User or both class User and class Course, but the Course fields are blank.

I even am trying linq.. this works - but only returns 1 row result

Dim uc =  From _u In _users.AsQueryable(Of User)()
    Where _u.userid = _userid _
    Select _
CourseID = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseId)(0), _
CourseDescription = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseDescription)(0)

Seems easy enough to do, just can't get it

Upvotes: 0

Views: 45

Answers (1)

user3146613
user3146613

Reputation: 11

Got It, I think I was over thinking it

Once I declare an instance of the class, I can iterate through the subdocument

Dim _u as new User
For Each c In _user.Courses.Where(Function(cs) cs.Status= "Active").Select(Function(cs) cs) 
console.writeline(c.id & c.description & "so on...")
Next

Upvotes: 0

Related Questions