I am working on a web app (using Mongoose).
This is my Users JSON:
[
{
_id:'2819vnablj3490fa',
name:'Bob',
subscriptions:[
{
_id:'s3989408121',
plan:'Premium',
services:[
{
_id:'2237fdas2323',
service_name:'Insurance',
},
{
_id:'37280172918',
service_name:'Consulting',
},
{
_id:'27847378172',
service_name:'Consulting',
}
]
}
]
}
]
In my example I would like to:
- Get user with id= 2819vnablj3490fa (Bob)
- Get subscription with id = s3989408121
- Add a new service ({_id:'1920910212',service_name:'Scouting'}) to services
I tried to do the following:
var new_service = {id:'1920910212',service_name:'Scouting'}
Label.findOneAndUpdate(
{ _id: "2819vnablj3490fa","subscriptions._id":"s3989408121"},
{ $addToSet: {services:new_service} }
);
Actually, this is one of the many solutions that I have adopted but I still can't get out of it.
Could u help me ?