【问题标题】:how to push data in inside array mongodb?如何将数据推送到数组 mongodb 中?
【发布时间】:2017-09-19 07:02:47
【问题描述】:

我正在尝试将数组推送到我的集合中的文档数组中

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

【问题讨论】:

  • 这里的错误信息是什么
  • Uncaught Error: After filtering out keys not in the schema, your modifier is now empty

标签: arrays mongodb meteor meteor-blaze


【解决方案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
    }
});

【讨论】:

  • 同样的错误我的错误是在架构中。
  • 你能检查我的架构吗
  • @kumbhaniBhavesh 你是对的。检查更新的答案。
猜你喜欢
  • 1970-01-01
  • 2020-05-21
  • 2020-06-19
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 2017-03-16
相关资源
最近更新 更多