Search code examples
arraysmongodbmeteormeteor-blaze

how to push data in inside array mongodb?


i am trying to push array in document array my collection is

{
"_id": "58eed81af6f8e3788de703f9",
"first_name": "abc",
"vehicles": {
    "exhibit": "18",
    "title": "Motor Velicle Information for Donald French",
    "details": [
        {
            "year": "",
            "make_model": "",
            "registered_owner": "",
            "license_number": "",
            "date_of_purchase": "",
            "purchase_price": ""
        }
    ]
}

}

so what i want is to push data in details for that i had try like this

 Licensee.update({"_id":"58eed81af6f8e3788de703f9"},{
        $push:{
            "vehicles.details":data
        }
    },function(err,data){
        if(!err)
        {
            console.log('data',data);
        }
        else
        {
            console.log('err',err);
        }
    });

and for this i create one schema i don't know is right or not

var licSchema = new SimpleSchema({
"_id":{
    type:String,
    label:"_id",
    optional: false,
},
"vehicles.details.year": {
    type: String,
    label: "year",
    optional: true,
},
"vehicles.details.make_model": {
    type: String,
    label: "make_model",
    optional: true,
}

});

where is my fault please give me solution . Error Uncaught Error: After filtering out keys not in the schema, your modifier is now empty


Solution

  • You can try this. AddToSet should be the right way.

    const schema = new SimpleSchema({
        "vehicles.details.$.year": {
            type: String,
            label: "year",
            optional: true,
         },
        "vehicles.details.$.make_model": {
            type: String,
            label: "make_model",
            optional: true,
         }
     });
    
    Licensee.update({"_id":"58eed81af6f8e3788de703f9"},{
        $addToSet:{
            "vehicles.details": data
        }
    });