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

180
Views
Comprobando si un campo contiene un valor de la lista de cadenas

Necesito preparar una consulta que ejecutará expresiones regulares para verificar si contiene resultados. Tengo dos documentos en la colección con employeeIds: Rabin y Begin. Quiero encontrar documentos con el nombre del empleado que contenga List("ab", "it"). La consulta mongo relevante:

 db.tasks.find( { employeeId : {$in : [/ab/, /it/]} } )

Devuelve un documento de resultado con employeeId de "Rabin". La pregunta es ¿cómo estoy convirtiendo esta consulta en código Scala? He implementado el siguiente código de prueba:

 val fieldName: String = "employeeId" val value: List[String] = List("ab", "it") val formattedVals = value.map ("/".concat(_).concat("/")) val mySelector = BSONDocument(fieldName -> BSONDocument("$in" -> formattedVals)) println(s"formattedVals: $formattedVals") println(BSONDocument.pretty(mySelector)) collection.findAndUpdate( selector = mySelector, update = BSONDocument("$set" ->BSONDocument( "CompanyId" -> "1000")), fetchNewObject = true ).map(el => el.result[GatorTask].getOrElse { throw new NoSuchElementException(s"No Document was found to match the query") })

PERO se arroja el resultado de la excepción ... Las salidas impresas:

 formattedVals: List(/ab/, /it/) { 'employeeId': { '$in': [ '/ab/', '/it/' ] } }

La consulta mongo con comillas simples alrededor de los parámetros "recortados" no devuelve ningún resultado. ¿Alguna idea de cómo implementar la consulta requerida en Scala?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes hacerlo de la siguiente manera:

 val formattedVals = value.map(value => s"(.*)$value(.*)").mkString("|")

y use $regex (en lugar de $in )

Por ejemplo, en tu caso:

 val mySelector = BSONDocument(fieldName -> BSONDocument("$regex" -> formattedVals)) val mySelector = BSONDocument(fieldName -> BSONDocument("$not" -> BSONDocument("$regex" -> formattedVals)))
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!