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

148
Views
Use a variable as a field specifier for a projection inside a find()

I want use a variable in Key inside a find() in mongodb. I want to do a request similar to select value from table;.

I tried this: value = '{ "' + axey.options[i].value + '" : 1, "_id" : 0 }';

and I do my request in this way : Validation.find({}, value ).fetch();

Someone know how can I use my value like a "Key" and not like a value ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use the bracket notation to construct the projection document. You also need to include the field specifier in your projection document. For example:

var projection = { "fields": { "_id": 0 } };
projection["fields"][axey.options[0].value] = 1;
Validation.find({}, projection ).fetch();
over 4 years ago · Santiago Trujillo Report

0

I didn't understand your question clearly though if you want to know if certain key in your documents exists as you mentioned select key in table than you can do by this.

Collection.find("keyName": {$exists:true}).fetch();
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!