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

259
Views
change values of nested objects by id with mongoose

I'm new to MongoDB, and I'm trying to do a very simple task, but however I can't get it right.

What I want is to change the process status but I tried "FindAndUpdate", "UpdateOne" and "FindByIdAndUpdate" but it won't work.

Maybe it has to do with my Schema. Should I create a new Schema for the Process?

My Database entry inside a MongoDB Collection:

_id: 622c98cfc872bcb2578b97a5
username:"foo"
__v:0
 process:Array
  0: Object
   processname:"bar"
   process_status:"stopped"
   _id: 6230c1a401c66fc025d3cb88

My current Schema:

const User = new mongoose.Schema(
    {
        username: { type: String, required: true },
        process: [
            {
                processname: {
                    type: String,
                },
                process_status: {
                    type: String,
                },
            },
        ],
    },
    { collection: "user-data" }
);

My current code:

const startstopprocess = await User.findByIdAndUpdate(
            { _id: "6230c1a401c66fc025d3cb88" },
            { process_status: "started" }
        ).then(function (error, result) {
            console.log(error);
            console.log(result);
        });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use positional operator $ in this way:

db.collection.update({
  "process._id": "6230c1a401c66fc025d3cb88"
},
{
  "$set": {
    "process.$.process_status": "started"
  }
})

Note how using positional operator you can say mongo "from the object you have found in find stage, update the process_status variable to started"

Example here

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!