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

165
Views
How to concatenate mongodb properties in nodejs variable

I'm attempting to concatenate the mongodb query in a variable in node js depending on my condition. I'm unable to concatenate the mongodb attributes here. I've included the output I'm looking for below. I understand that the + icon only concatenates strings.

  • What i need is:
var query;
query += db.collection('collection_name');
query += query.find();
if(project == true)
{
    query +=query.project(project_parameter);
}

if(limit == true)
{
    query += query.limit();
}

// Output needed if i run the query variable
db.collection('collection_name').find({id:1}).project({_id:1}).limit(10).toArray();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If these functions support chaining, then you can do something similar to this:


let query = db.collection('collection_name')
query = query.find()
if (project === true) {
  query = query.project(project_parameter)
}

if (limit === true) {
  query = query.limit()
}

This is because calling a function on db.colllection('collection_name') returns a modified object that you can keep calling functions on.

about 4 years ago · Juan Pablo Isaza 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!