0

我有以下文件:

mainDoc = {
        owner: Meteor.userId(),
        createdOn: new Date(),
        active: false,
        label: "Dashboard #" + ($("ul#u-nav-tabs").find("li.u-tab").length + 1),
        monitors: [/*Embedded documents*/],
        sharewith: []
    };

mainDoc.monitors是以下文档的数组:

innerDoc = {
                _id: id._str,
                owner: Meteor.userId(),
                createdOn: new Date(),
                label: monitorLabel,
                metadata: {custDate: {}},
                style: {
                    top: mystyle.top,
                    left: mystyle.left,
                    width: 0,
                    height: 0
                },
                shown: true,
                sharewith: []
            }

我在服务器上设置了以下权限

userDashboards.allow({
    insert: function (userId) {
        "use strict";
        return userId;
    },
    update: function (userId, doc) {
        "use strict";
        return doc.owner === userId;
    },
    remove: function (userId, doc) {
        "use strict";
        return doc.owner === userId;
    },
    fetch: ["owner", "monitors"]
});

到目前为止,我在客户端上试过这个:

console.log(userDashboards.findOne({"monitors._id": "5f94f2a15bddd908f2bc9d5d"}));

但我只得到完整的文档,而不是嵌入的文档。

所以问题是,我怎样才能innerDoc.style直接从浏览器更新?

4

1 回答 1

0

试试这个。

更新

userDashboards.update({_id:"5f94f2a15bddd908f2bc9d5d"},{$set:{'monitors[0]._id':"yeaaa look mom im editing documents with arrays"}})

假设“5f94f2a15bddd908f2bc9d5d”._id不像monitors._id. 你说的监视器是一个数组,所以你应该指定索引aka [0]。记住更新的语法应该是这样的。

    Collection.update({_id:idDocument},{$set:{fieldToUpdate:newData}}) // correct

Collection.update({_id:idDocument,$set:{fieldToUpdate:newData}}) // wrong, you will get some "stack error" 

Collection.update({$set:{fieldToUpdate:newData}}) // wrong, you need to specify what document will be update and by stric the id should be always {_id:"5f94f2a15bddd908f2bc9d5d"} <-- like this

那应该行得通。

于 2015-01-27T21:49:12.963 回答