如何将数据推送到数组 mongodb 中?

how to push data in inside array mongodb?

我正在尝试将数组推送到文档数组中,我的 collection 是

{
"_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": ""
        }
    ]
}

}

所以我想要的是在 details 中推送数据,因为我曾尝试过这样

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

为此我创建了一个我不知道正确与否的模式

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,
}

});

我的错在哪里请给我解决办法。 错误 Uncaught Error: After filtering out keys not in the schema, your modifier is now empty

你可以试试这个。 AddToSet 应该是正确的方法。

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
    }
});