Meteor Mongo添加子文档

时间:2015-09-19 12:59:32

标签: mongodb meteor

我有一个这样的集合文档MasterPropinsis

{
    "_id": "4HSb7bbjFBzRSftXu",
    "nama": "Yogyakarta",
    "kabupaten": [
        {
            "id": "KulonProgo",
            "nama": "Kulon Progo",
            "kecamatan": [{ "nama": "Kalibawang" },{ "nama": "Nanggulan" }]
        },
        {
            "id": "Sleman",
            "nama": "Sleman",
            "kecamatan": [{ "nama": "Depok" },{ "nama": "Berbah" }]
        },
        {
            "id": "Bantul",
            "nama": "Bantul",
            "kecamatan": []
        }
    ]
}

kabupaten:Bantul,我想使用以下代码添加子文档kecamantan:XXX

Masterpropinsis.update(
    {
        _id: Session.get('idKabupaten').toString(),
        'kabupaten.id': Session.get('idKecamatan').replace(" ", "")
    },
    {
        $addToSet: {
            'kabupaten.kecamatan': {
                nama: nama,
                createdAt: new Date(),
                createBy: CreateBy,
                createByID: CreateByid
            }
        }
    },
    {
        validate: true
    });

但是我收到了这个错误:

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

1 个答案:

答案 0 :(得分:1)

这是一个尝试此

的示例
width=...

你可以看到我们只在选择器中提供var docId = Session.get('idKabupaten'); // usally toString is not nesserry unless you do something unusual Masterpropinsis.update({_id: docId }, { $push: { 'kabupaten.kecamatan': { nama: nama, createdAt: new Date(), createBy: CreateBy, createByID: CreateByid } } }); // validate true also is the default unless again you do something unusual 错误在客户端中说明,你只能通过服务器中的ID来更新你可以做任何选择器。还希望您可以使用允许/拒绝规则来允许来自客户端的更新? http://docs.meteor.com/#/full/allow