I am very new to NoSQL world. Here the screenshot of my Mongoose schema.
I need to insert,update and delete document to/from vehicles array.
What I have tried so far:
Add: (Working)
Companies.findOne({'mobile' : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId}, (err, res) => {
if( err || res == null) {
callback(err, res);
}else{
var len = res.VehicleGroups.length;
for(var i=0; i<len; i++)
{
if(res.VehicleGroups[i].vehicle_group_id == vehicleGroupId)
res.VehicleGroups[i].vehicles.push(data);
}
res.save(callback);
}
})
Delete: (Working)
Companies.findOneAndUpdate({ mobile : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId},
{ $pull : {'VehicleGroups.$.vehicles' : { 'vehicle_id' : vehicleId} } }, callback);
Still working to update data. Is my approach valid?
Thanks