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

279
Views
MongoDB Obtener la longitud del valor del campo

Ya he visto estos mensajes:

Longitud del valor del campo de cadena en mongoDB

Seleccione la longitud de la cadena en mongodb

Pero mi problema es diferente. Cuando estoy ejecutando esta consulta:

 db.usercollection.find({$where: "this.profile.name.length < 20"}).limit(2);

Recibo el siguiente error:

 Error: error: { "ok" : 0, "errmsg" : "TypeError: this.profile is undefined :\n_funcs2@:1:24\n", "code" : 139 }

cuando estoy ejecutando esta consulta:

 db.usercollection.find({"profile.name": {$exists: true}, $where: "this.profile.name.length <20"}).limit(2);

Sin error, pero resultado vacío, aunque mi colección tiene 2 documentos donde profile.name<20

MongoDB shell versión: 3.2.11

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Hay un documento donde la clave profile (documento incrustado) no existe, y la expresión de javascript arroja el error ya que intenta acceder a un atributo inexistente de un objeto:

db.usercollection.find({$where: "this.profile.name.length < 20"}).limit(2);

Su segunda consulta funciona bien, porque coincide con todos los documentos donde existe profile.name :

 db.usercollection.find({"profile.name": {$exists: true}, $where: "this.profile.name.length <20"}).limit(2);

La segunda consulta debería devolver el resultado deseado.

over 4 years ago · Santiago Trujillo Report

0

ese perfil.nombre se puede verificar en la cláusula where en sí

 db.usercollection.find({$where: "this.profile && this.profile.name && this.profile.name.length < 20"}).limit(2);
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!