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

251
Views
How do I set values in a nested map in mongoose directly

I have a mongoose schema which has a map(holding) nested in other map(users).

const UserSchema = new mongoose.Schema({
  users: {
    type: Map,
    of: new mongoose.Schema({
      holding: {
        type: Map,
        of: Number,
      },
    }),
  },
});
const UserModel = mongoose.model('black', UserSchema);

I create a new user with the below info.

  var user = new userModel({
    users: {
      jose: {
        holding: { itc: 9, hcl: 300 },
      },
    },
  });

Now I am trying to edit a value in the map holdings for my user with the set method. Which I am able to do as below.

  await user.set(`users.jose`, {
    holding: { itc: 90, hcl: 300 },
  });
  user.get(`users.jose.holding.itc`) //displays new value(90)

However if I try to set the values in nested map "holding" directly this does not work.

ie the below code fails(value is not set)

await user.set(`users.jose.holding.itc`, 1000);
user.get(`users.jose.holding.itc`) //gives old value or does not display 1000

Why does this fail? .How do I set values in a nested map in mongoose directly.

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