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

66
Views
Incrementing multiple array values in an object MongoDB

I've been at this for a while now. Trying to update multiple array values, in the same object, with a single query.

Database data looks like this:

  id: 616f5aca5f60da8bb5870e36
  title: "title"
  recommendations: {
    616f65705f60da8bb5870e37: [
      0: 25,
      1: "title 2"
    ],
    61e83b48e498cc5aae9dc741: [
      0: 22,
      1: "title 3"
    ]
  }

So far I've tried both preparing a statement like this:

const fetchQuery = "recommendations.616f65705f60da8bb5870e37.0 : 1, recommendations.61e83b48e498cc5aae9dc741.0 : 1";

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { $inc: fetchQuery }
      );

And using array filters:

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { $inc: {"recommendations.$[elem].0": 1} },
        {arrayFilters: [{"elem":{$in:[req.body.voteIds]}}]}
      );

But neither seems to work. Is this possible or is my only option converting to array of arrays instead?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to enclose recommendations.616f65705f60da8bb5870e37.0 in quotes, like:

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { 
          $inc: {
             "recommendations.616f65705f60da8bb5870e37.0": 1,
             "recommendations.61e83b48e498cc5aae9dc741.0": 1 
        }
      );
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!