Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
MongoDB: el operador de función $ no admite la función de flecha

Tengo los siguientes documentos en testCollection :

 [ { "_id": ObjectId("60562f98d171d52ef0a5bb27"), "popularDate": ISODate("1947-08-15T00:00:00.000+05:30") }, { "_id": ObjectId("60562f98d171d52ef0a5bb28"), "popularDate": ISODate("1950-01-26T00:00:00.000+05:30") }, { "_id": ObjectId("60562f98d171d52ef0a5bb29"), "popularDate": ISODate("1994-01-15T00:00:00.000+05:30") } ]

Estoy usando expresiones regulares para filtrar documentos usando el operador $function . Obtengo el resultado correcto mientras uso Query 1 .

Consulta 1 :

 let yearRegex = /^1947/; db.testCollection.find({ $expr: { $function: { body: function(popularDates, yearRegex) { return yearRegex.test(popularDates) }, args: [{ $toString: "$popularDates" }, yearRegex], lang: "js" } } });

Salida para la consulta 1 :

 { "_id" : ObjectId("60562f98d171d52ef0a5bb27"), "popularDate" : ISODate("1947-08-15T00:00:00.000+05:30") }

pero para la consulta 2 obtengo todos los documentos y el filtro no funciona. En la Consulta 2 , cambié el cuerpo de la función a la función de flecha.

Consulta 2 :

 let yearRegex = /^1947/; db.testCollection.find({ $expr: { $function: { body: (popularDate, yearRegex) => yearRegex.test(popularDate), args: [{ $toString: "$popularDate" }, yearRegex], lang: "js" } } });

Salida para la consulta 2 :

 { "_id": ObjectId("60562f98d171d52ef0a5bb27"), "popularDate": ISODate("1947-08-15T00:00:00.000+05:30") }, { "_id": ObjectId("60562f98d171d52ef0a5bb28"), "popularDate": ISODate("1950-01-26T00:00:00.000+05:30") }, { "_id": ObjectId("60562f98d171d52ef0a5bb29"), "popularDate": ISODate("1994-01-15T00:00:00.000+05:30") }

Así que ahora mi pregunta es por qué la función de flecha no funciona dentro del operador $function , o me estoy perdiendo algo.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

MongoDB se basa en el uso de la call javascript para establecer this en el documento actual al llamar a la función pasada.

Las funciones de flecha no tienen vínculos con this o super (consulte https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions ), por lo que no funcionan correctamente en el lado del servidor javascript en MongoDB.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!