skip to Main Content

I want to ges all the documentos that are inside a range of dates, but my query instó retriving all the validar Collections:

To resume
My query receives two date values ​​as a string, one for ‘arrival’ and the other for ‘check-out’ with these two dates i need to retrive all documents that are between these two dates.

Ex.
arrival=’2024-06-11T06:00:00.000Z’
check-out=’2024-06-16T06:00:00.000Z’

with these values i should retrive all documents in which the ‘arrival’ value is ‘greater than or equal’ to the arrival input and ‘less than’ the ‘check-out’ value, but it should also get all the documents in which the ‘check-out’ value is ‘greater than’ the the arrival input

And i dont know how to the $or clase in the query for the ‘check-out’ case

My Query:

db.collection.find({
  $expr: {
    $and: [
      {
        $gte: [
          {
            $dateFromString: {
              dateString: "$arrival"
            }
          },
          new Date("2024-06-11T06:00:00.000Z")
        ]
      },
      {
        $lt: [
          {
            $dateFromString: {
              dateString: "$arrival"
            }
          },
          new Date("2024-06-16T06:00:00.000Z")
        ]
      }
    ]
  }
})

Mongo playground Link : https://mongoplayground.net/p/1HRTg2eLeDa

2

Answers


  1. Chosen as BEST ANSWER

    Finally manage to complete the query, thanks to the anwsers of everyone i manager to come up with the solution, this is how the query ended op like:

    db.collection.find({
    $expr: {
        $or: [
          {
            $and: [
              {
                $gte: [
                  {
                    $dateFromString: {
                      dateString: "$arrival"
                    }
                  },
                  new Date("2024-06-11T06:00:00.000Z")
                ]
              },
              {
                $lt: [
                  {
                    $dateFromString: {
                      dateString: "$arrival"
                    }
                  },
                  new Date("2024-06-16T06:00:00.000Z")
                ]
              }
            ]
          },
          {
            $and: [
              {
                $gte: [
                  {
                    $dateFromString: {
                      dateString: "$checkout"
                    }
                  },
                  new Date("2024-06-11T06:00:00.000Z")
                ]
              },
              {
                $lt: [
                  {
                    $dateFromString: {
                      dateString: "$checkout"
                    }
                  },
                  new Date("2024-06-16T06:00:00.000Z")
                ]
              }
            ]
          }
        ]
      }
    })
    

  2. If you store values as Date objects and if I understand you requirements correctly, it would be this one:

    db.collection.find({
       $or: [
          { arrival: { $gte: new Date("2024-06-11T06:00:00.000Z"), $lt: new Date("2024-06-16T06:00:00Z") } },
          { "check-out": { $gte: new Date("2024-06-11T06:00:00.000Z"), $lt: new Date("2024-06-16T06:00:00Z") } },
       ]
    })
    

    The $and operator is not needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search