Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
MongoDB Get length of field value

I have already seen these posts:

String field value length in mongoDB

Select string length in mongodb

But My problem is different. When I am running this query:

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

I am getting the below error:

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

when I am running this query:

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

No error, But empty result, though my collection have 2 documents where profile.name<20

MongoDB shell version: 3.2.11

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There is a document where profile key (embedded document) does not exist, and javascript expression is throwing the error since is trying to access an unexisting attribute of an object:

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

Your second query works OK, because it matches all documents where profile.name exists:

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

The second query should return the desired result.

over 4 years ago · Santiago Trujillo Denunciar

0

that profile.name can be checked in where clause itself

db.usercollection.find({$where: "this.profile && this.profile.name && this.profile.name.length < 20"}).limit(2);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda