I have a nested objects like this:
{
fits: {
honda: {
shadows: {
2000: true,
2001: true,
2003: true
}
}
}
}
I want to add new models so it would look something like this:
{
fits: {
honda: {
shadows: {
2000: true,
2001: true,
2003: true
},
aaaa: {},
bbbb: {}
...
}
}
}
and later years:
{
fits: {
honda: {
shadows: {
2000: true,
2001: true,
2003: true
},
aaaa: {
1990: true,
...
},
bbbb: {}
...
}
}
}
I am able to add new makes:
{
fits: {
honda: {
shadows: {
2000: true,
2001: true,
2003: true
},
},
kawasaki: {},
...
}
}
But when I want to add a new model for example to honda it will erase the previous one and than put in new one:
{
fits: {
honda: {
aaaa: {},
}
}
}
For adding new makes I am using this code and it works just fine:
data.fits[newMake] = {}
this.state.client.auth.loginWithCredential(new AnonymousCredential()).then((user) => {
this.state.db.collection('products').findOneAndUpdate(
{_id: data._id}, {$set:{fits: data.fits}}, {returnNewDocument: true}
).then((result)=>{
this.setState({
rowData: result,
})
}).catch((e)=>console.log(e))
})
Here is the one i use for inputing new models, but it keeps erasing the other model so I'm always left with just one model:
var query = {}
query[make] = {}
query[make][addedModel] = {}
this.state.client.auth.loginWithCredential(new AnonymousCredential()).then((user) => {
this.state.db.collection('products').findOneAndUpdate(
{_id: _id},{$set:{fits: query}},{returnNewDocument: true}
).then((result)=>{
console.log(result)
this.props._handleAddModel(result)
}).catch((e)=>console.log(e))
});
I think that $set data just sets the new data without preserving the old, but than why it works on makes and not on models? Am I understanding the $set wrong?
I looked for other atomic operators but couldn't find anything that would just add the new field without erasing other fields.