title: MongoDB How to Find out the records which are not in between the date range
body: <p>I have two collections like below,</p>
  
  <p><strong>user Collection</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("5af2e946aa546125b5de85cc"),
      "name" : "Sudhin",
      "email" : "abc@abc.com",
      "roles" : [ 
          "Reader", 
          "Instructor"
      ],
      "createdAt" : ISODate("2018-05-09T12:27:50.651Z"),
      "updatedAt" : ISODate("2018-05-16T09:22:07.280Z")
  },
  {
      "_id" : ObjectId("5af2f3a6efb83031faaa3d82"),
      "name" : "Rahul",
      "email" : "abcd@abc.com",
      "roles" : [ 
          "Reader", 
          "Instructor"
      ]
      "createdAt" : ISODate("2018-05-09T13:12:06.518Z"),
      "updatedAt" : ISODate("2018-05-16T09:22:07.281Z")
  } 
  </code></pre>
  
  <p><strong>schedulers Collections</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("5afd763b8fad29597e1b85ed"),
      "title" : "ILT Course",
      "type" : "Course",
      "ilt" : {
          "instructorId" : ObjectId("5af2e946aa546125b5de85cc"),
          "type" : "ILT-Offline",
          "instructorName" : "Sudhin",
          "place" : "*******",
          "description" : "******"
      },
      "startDate" : ISODate("2018-05-10T11:00:00.000Z"),
      "endDate" : ISODate("2018-05-15T12:00:00.000Z"),
      "createdAt" : ISODate("2018-05-17T12:31:55.574Z"),
      "updatedAt" : ISODate("2018-05-17T12:31:55.575Z")
  }
  </code></pre>
  
  <p>In the scheduler collection "ilt.instructorId" is the referenceId for user.</p>
  
  <p>Scheduler collection is having all the details of a particular user schedule.</p>
  
  <p>startDate is the starting date and time of a particular schedule.
  endDate is the ending date and time of a particular schedule.</p>
  
  <p>When I pass startDate to endDate(2018-05-05 00:00 -  2018-05-10 00:00), I want to fetch all the users with role instructor and who do not have any scheduled courses in between those dates. </p>
  
  <p>Eg: If I'm passing  2018-05-05 00:00 -  2018-05-10 00:00 it should return
  the below document</p>
  
  <pre><code>   {
          "_id" : ObjectId("5af2f3a6efb83031faaa3d82"),
          "name" : "Rahul",
          "email" : "abcd@abc.com",
          "roles" : [ 
              "Reader", 
              "Instructor"
          ]
          "createdAt" : ISODate("2018-05-09T13:12:06.518Z"),
          "updatedAt" : ISODate("2018-05-16T09:22:07.281Z")
      }
  </code></pre>
  
  <p>I have tried the below query</p>
  
  <pre><code>UserModel.query().aggregate([
  
      { $match: { roles: { $in: ['Instructor'] } } },
  
      {
          $lookup: {
              from: "schedulers",
              localField: "_id",
              foreignField: "ilt.instructorId",
              as: "schedule"
          }
  
      },
  
      {
          $match: {
              "schedule.type": "Course",
              $and: [
  
                  { 'schedule.endDate': { $not: { $lte: new Date("2018-05-12T12:00:00.000Z") } }},
                  { 'schedule.startDate': { $not: { $gte: new Date("2018-05-06T11:00:00.000Z") } }},
              ]
          }
      },
  
  
      {
          $project: {
              _id: 1,
              name: 1,
              empId: 1,
              startDateTime: "$schedule.startDate",
              endDateTime: "$schedule.endDate",
      }
  }])
  </code></pre>