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

275
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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