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.