et 从 MongoDB 返回整个子文档数组
Posted
技术标签:
【中文标题】et 从 MongoDB 返回整个子文档数组【英文标题】:et Return the Whole Subdocument Arrary from MongoDB 【发布时间】:2014-05-28 13:32:08 【问题描述】:我需要从 User.UserId = 不管和 Courses.Status=active 的 Courses 类中获取所有子文档数组
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
使用 vb.net ,我尝试了几种方法,我只希望将返回的具有 Status="Active" 的课程转储到 Ienumberable 中
我试过了(_users是User的集合)(_uid是传入的变量)
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")
似乎都不起作用,他们通常会返回来自班级用户或班级用户和班级课程的字段,但课程字段是空白的
我什至在尝试 linq.. 这有效 - 但只返回 1 行结果
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)
看起来很简单,就是搞不定
【问题讨论】:
【参考方案1】:知道了,我想我想多了
一旦我声明了一个类的实例,我就可以遍历子文档
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
【讨论】:
以上是关于et 从 MongoDB 返回整个子文档数组的主要内容,如果未能解决你的问题,请参考以下文章
MongoDB find() 在匹配(字段,值)时返回子文档