如何在内部数组mongodb中推送数据?

时间:2017-04-22 08:13:30

标签: arrays mongodb meteor meteor-blaze

我正在尝试在文档数组中推送数组,我的集合是

{
"_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

1 个答案:

答案 0 :(得分:1)

你可以试试这个。 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
    }
});