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

274
Views
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 answers
Answer question

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 Report

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