I want to insert a new element the "CarList" array below. I am using MongoDB and dropwizzard in my project.
{
    "_id": "56aa6119bf78f37eee64697e",
    "name": "Jack",
    "occupation": "business*emphasized text* owner",
    "carList": [{
        "brand": "Ford",
        "cost": "$25000"
    }, {
        "brand": "Mazda",
        "cost": "$23000 "
    }]
}
My POJO classes:
public class CarOwners {
    @ObjectId
    public String _id;
    public String name;
    public String occupation;
    public List<CarDetails> carList;
}
public class CarDetails
{
    public String brand;
    public String cost;
}
My code to append a new entry in carList:
CarDetails newCarInfo = new CarDetails();
newCarInfo.brand="Toyota";
newCarInfo.cost="$20000";
BasicDBObject ownerQuery = new BasicDBObject("name", "Jack");
carOwnerCollections.update(ownerQuery, new BasicDBObject("$addToSet", new BasicDBObject("carList",newCarInfo)));
The error that I am getting:
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class data.CarDetails] from JSON String; no single-String constructor/factory method (through reference chain: data.PropertyLedger["carList"])
From the error I can guess that the the wiring to CarDetails class is the issue. I am not sure how to solve it. I have looked in updating nested documents in Mongo and similar links but could not find an answer.
Any pointers will be helpful. Thank you.
 
                        
Try doing it using the
$pushoperator for arrays in MongoDB.