Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Filter data before lookup(join) two collections in mongodb

Filter data before lookup(join) two collections in mongodb

Scheduled Pinned Locked Moved Database
databasemongodbregexquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8 Offline
    8 Offline
    855
    wrote on last edited by
    #1

    I'm writing a function which will join two collections and two filters for each.

    public class Teacher
    {
    public string Id{ get; set; }
    public string Name{ get; set; }
    public string TeacherFilter{ get; set; }
    ...
    }

    public class Student
    {
    public string Id{ get; set; }
    public string Name{ get; set; }
    public string StudentFilter{ get; set; }
    public string TeacherId{ get; set; }
    ...
    }

    public class TeacherStudents
    {
    public string Id{ get; set; } -- from teacher
    public string Name{ get; set; } -- from teacher
    public IEnumerable<Student> Students{get;set;}
    }

    public class TeacherStudents
    {
    public string Id{ get; set; } -- from teacher
    public string Name{ get; set; } -- from teacher
    public IEnumerable<Student> Student{get;set;}
    }

    public class TeacherStudent
    {
    public string Id{ get; set; } -- from teacher
    public string Name{ get; set; } -- from teacher
    ...
    public Student Student{get;set;}
    }

    Get(string TeacherFilter,string StudentFilter).

    Here is how i implement it

    var query = teacherCollection.Aggregate().Match(x => x.TeacherFilter== TeacherFilter); //filter teach
    var query1 = query
    .Lookup<Teacher, Student, TeacherStudent>(studentCollection, t => t.Id, s => s.TeacherId, l => l.Student)
    .Unwind(x => x.Student, new AggregateUnwindOptions<TeacherStudent>())
    .Match(x => x.StudentFilter== StudentFilter) //filter student

    For the teacher collection, useless data has been filter out which will reduce the data size when doing the lookup(join). But for the student collection, the match stage is appended after the lookup. so does all the data from student will join with teacher behind the scenes? is it possible to filter out some student data before the lookup?

    R 1 Reply Last reply
    0
    • 8 855

      I'm writing a function which will join two collections and two filters for each.

      public class Teacher
      {
      public string Id{ get; set; }
      public string Name{ get; set; }
      public string TeacherFilter{ get; set; }
      ...
      }

      public class Student
      {
      public string Id{ get; set; }
      public string Name{ get; set; }
      public string StudentFilter{ get; set; }
      public string TeacherId{ get; set; }
      ...
      }

      public class TeacherStudents
      {
      public string Id{ get; set; } -- from teacher
      public string Name{ get; set; } -- from teacher
      public IEnumerable<Student> Students{get;set;}
      }

      public class TeacherStudents
      {
      public string Id{ get; set; } -- from teacher
      public string Name{ get; set; } -- from teacher
      public IEnumerable<Student> Student{get;set;}
      }

      public class TeacherStudent
      {
      public string Id{ get; set; } -- from teacher
      public string Name{ get; set; } -- from teacher
      ...
      public Student Student{get;set;}
      }

      Get(string TeacherFilter,string StudentFilter).

      Here is how i implement it

      var query = teacherCollection.Aggregate().Match(x => x.TeacherFilter== TeacherFilter); //filter teach
      var query1 = query
      .Lookup<Teacher, Student, TeacherStudent>(studentCollection, t => t.Id, s => s.TeacherId, l => l.Student)
      .Unwind(x => x.Student, new AggregateUnwindOptions<TeacherStudent>())
      .Match(x => x.StudentFilter== StudentFilter) //filter student

      For the teacher collection, useless data has been filter out which will reduce the data size when doing the lookup(join). But for the student collection, the match stage is appended after the lookup. so does all the data from student will join with teacher behind the scenes? is it possible to filter out some student data before the lookup?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      You have already posted this in QA: Filter data before lookup(join) two collections in mongodb[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      8 1 Reply Last reply
      0
      • R Richard Deeming

        You have already posted this in QA: Filter data before lookup(join) two collections in mongodb[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        8 Offline
        8 Offline
        855
        wrote on last edited by
        #3

        why no one answer it?

        R 1 Reply Last reply
        0
        • 8 855

          why no one answer it?

          R Offline
          R Offline
          Richard Deeming
          wrote on last edited by
          #4

          You posted it less than 12 hours ago. Half the world won't have woken up and had a chance to to read it yet. And since questions here are answered by volunteers, nobody is under any obligation to answer it at all, let alone within a defined time limit.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups