Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
Can $regex and $gte/$lte for strings in mongodb be used together?

I've transaction date in mongodb database in the format "7/1/2021 12:30:48 PM" (string in M/D/YYYY). From React side, I'm entering two dates : From date and To Date which are both strings in the format YYYY-MM-DD. I'm using the below code, to find transactions that come between From and To dates. I'm getting a cast error but all the dates are in string format. It works perfectly with only one date without $gte and $lte.

ERROR Message: CastError: Cast to string failed for value "{ '$regex': '7/1/2021', '$options': '$i' }" (type Object) at path "Trandate" for model "transaction"

const chooseWinner = await Transaction.find(
    {
      ContCode: CountryCode,
      CurrCode: CurrCode,
      CorrOrgCode: CorrorgCode,
      ServCode: ServCode,
      BranchCode: BranchCode,
      Trandate: {$gte: {$regex : SDate, $options: "$i"}},
      Trandate: {$lte: {$regex : EDate, $options: "$i"}},
    },
    "CustomerCode ReferenceNo Trandate "
  );

Also I've changed the from and to dates to the below format:

var SDate = new Date(FromDate).toLocaleDateString("hi-IN",{month:'numeric'})+"/"+
                  new Date(FromDate).toLocaleDateString("hi-IN",{day:'numeric'})+"/"+
                  new Date(FromDate).toLocaleDateString("hi-IN",{year:'numeric'});
  console.log("frmdate: ",SDate);
  

  var EDate = new Date(ToDate).toLocaleDateString("hi-IN",{month:'numeric'})+"/"+
                  new Date(ToDate).toLocaleDateString("hi-IN",{day:'numeric'})+"/"+
                  new Date(ToDate).toLocaleDateString("hi-IN",{year:'numeric'});
  console.log("enddate: ",EDate); 

      
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Don't Know about the regex and $gte/$lte together but it seems like issue is with date format. Why don't use use slandered

{Trandate:{$gte:ISODate(FromDate),$lt:ISODate(ToDate)}}

about 4 years ago · Juan Pablo Isaza Relatório

0

To answer the directly asked question, no, the $regex query operator cannot be nested inside an inequality operator like that.

The $regex operator returns true or false indicate whether or not the string matched, so even if it were permitted, that nested comparison would effectively be "less than or equal to (boolean)". Since mongodb query operators are type sensitive, that comparison will always be false.

To answer the implied question How can I query these dates by a specified range?:

Dates stored in the string format you show cannot be meaningfully compared with inequality operations.

For example, here are a few dates in sorted order:

    "6/1/2022 1:30 PM",
    "7/1/2021 12:30:48 PM",
    "7/1/2021 9:30:48 AM",
    "7/12/2021 12:30:48 PM",
    "7/2/2021 12:30:48 PM",
    "8/1/2001 8:55:00 AM"

If you were to query for {$lte: "8/1/2001 8:55:00 AM"} all 6 of those dates would match.

In order to query a range of dates, you will need to either:

  • Query with aggregation
    use a pipeline that reads every document in the collection, uses $dateFromString aggregation operator to convert the strings to dates, then match on the converted field. This is not scalable, and will not perform well under any non-trivial load.
  • Modify the schema
    Change the application to convert the string dates to a Date object before storing in the database. All existing documents will also need to be modified. This will permit you to query against the field with $gte and $lte directly, and allow an index to be utilized to improve performance.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda